测试方法包括上下文等
Testing methods including the context and etc
如何测试访问应用 context
的方法,例如为了从资源中获取字符串。
代码:
public String getString(Context context) {
String string = context.getResources().getString(R.id.string);
return string;
}
测试代码:
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);
}
如何处理上下文?
@Before
public void setUp(){
myClass = new MyClass();
context = InstrumentationRegistry.getTargetContext();
}
另见 the Android testing documentation and the InstrumentationRegistry
JavaDocs。
如何测试访问应用 context
的方法,例如为了从资源中获取字符串。
代码:
public String getString(Context context) {
String string = context.getResources().getString(R.id.string);
return string;
}
测试代码:
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);
}
如何处理上下文?
@Before
public void setUp(){
myClass = new MyClass();
context = InstrumentationRegistry.getTargetContext();
}
另见 the Android testing documentation and the InstrumentationRegistry
JavaDocs。