Issue
How to test method that accesses the app context, for example in order to get a string from resources.
Code:
public String getString(Context context) {
String string = context.getResources().getString(R.id.string);
return string;
}
Test code:
MyClass myClass;
Context context; // ???
@Before
public void setUp(){
myClass = new MyClass();
}
@Test
public void convertToMessage() throws Exception {
String myString = "My string";
String gettedString = myClass.getString(context);
assertEquals(myString, gettedString);
}
What to do with the context?
Solution
@Before
public void setUp(){
myClass = new MyClass();
context = InstrumentationRegistry.getTargetContext();
}
See also the Android testing documentation and the InstrumentationRegistry JavaDocs.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.