Reputation: 821
I am writing a test class for my non activity class:
public class SinglePhoto {
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
//---...
}
In my test class I need to access the context but I dont know how to do it:
public class SinglePhotoTest extends TestCase {
public void testDecodeSampledBitmapFromResource() throws Exception {
SinglePhoto sp = new SinglePhoto();
Context testContext = ??!
Bitmap bmp = sp.decodeSampledBitmapFromResource(testContext.getResources(), R.drawable.sample, 100, 100);
}
}
Can you help me please?
Thanks
Upvotes: 0
Views: 941
Reputation: 912
Instead of extending the TestCase extend the AndroidTestCase. And use the getContext() method to get the context. Here is the AndroidTestCase documentation: http://developer.android.com/reference/android/test/AndroidTestCase.html
Upvotes: 2
Reputation: 78
The best tool for testing static and other generally "inaccessible" things is probably PowerMockito. There is no short answer for how to use PowerMockito, so I enclose a link for reading: http://www.johnmullins.co/blog/2015/02/15/beginners-guide-to-using-mockito-and-powermockito-to-unit-test-java/
Also, extending TestCase is an old-fashion JUnit 3 approach, use the @Test annotation, used from JUnit 4.
Upvotes: 0