Reputation: 198318
I'm using Robolectric to test android code, and found there are some tests failed because there are some methods don't have shadow method.
In my logic code, I retrieved a bitmap from another activity:
Bitmap bitmap = getBitmapFromResult(data);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
And later in another method, I get the bitmap from the imageView
, and save it to file:
BitmapDrawable drawable = (BitmapDrawable)imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
// save to file
bitmap.compress(...);
But the last line throws NullPointerException when testing.
I debugged into the code, and found when the test code running with robolectric, the drawable
is a ShadowBitmapDrawable
contains a null
bitmap. So drawable.getBitmap()
return a null
which causes the exception.
Then I enabled logging in my test code:
Robolectric.logMissingInvokedShadowMethods();
Found one line in console which I think causes my test code failed:
No Shadow method found for BitmapDrawable.<init>(android.content.res.Resources, android.graphics.Bitmap)
What can I do now? Is it possible to add a shadow method to fix it and how to do that?
Upvotes: 0
Views: 329
Reputation: 198318
Clone the source from github: https://github.com/pivotal/robolectric/
Add a shadow method to ShadowBitmapDrawable.java
:
public void __constructor__(android.content.res.Resources res, Bitmap bitmap) {
this.bitmap = bitmap;
}
Then compile it and package it to a jar. It's fixed.
Upvotes: 2