我想为使用android Context的方法“getAge”编写单元测试。
StringUtil
public static String getAge(Context context, int age) {
String result = "";
result = context.getString(R.string.years_5);
return result;
}
我写单元测试:
StringUtilTest
@RunWith(MockitoJUnitRunner.class)
public class StringUtilTest {
@Mock Context mMockContext;
@Test
public void testGetAge() {
int ageTest = 5;
assertThat(StringUtil.getAge(mMockContext, ageTest), is(mMockContext.getString(R.string.years_5)));
}
但是当开始测试时我得到错误:
java.lang.RuntimeException:Stub!
at android.content.Context.getString(Context.java:14)
at my.company.StringUtil.getAge(StringUtil.java:49)
at my.company.StringUtilTest.testGetAge(StringUtilTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)