Reputation: 3167
public class JamTest {
Explosions EX = new Explosions(null);
private List<Bitmap> expList ;
Context mtx;
@Before
public void setUpList(){
expList = new ArrayList<Bitmap>(2);
Bitmap bmp1 = BitmapFactory.decodeResource(//resources here, R.drawable.ic_launcher);
expList.add(bmp1);
}
@Test
public void testInit(){
assertNull(EX.getListSize().get(0));
}
@Test
public void testGetlist(){
assertEquals("Result",0,EX.getListSize().size());
}}
This is a simple testcase I am trying to setup, but I need to get access to resources from the test class setUpList()
method. How do I access image resources for this Android Junit test? Thank you.
Upvotes: 4
Views: 6140
Reputation: 2883
You can use Robolectric to accomplish that.
For your code, you would have to put the following annotation:
@RunWith(RobolectricTestRunner.class)
public class JamTest {
...
and then
Bitmap bmp1 = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.ic_launcher);
You have a look at more usages of BitmapFactory using Robolectric here: http://searchcode.com/codesearch/view/5789498
Upvotes: 4