我们刚刚开始在 Android Studio 中为我们的项目实现单元测试,这个问题困扰了我 2 天。我的一位同事写了一个小的单元测试示例,看看 Context.getString() 是否返回一个特定的字符串。测试类主体如下:
private static final String FAKE_STRING = "some_string";
@Mock
Context context;
@Test
public void readStringFromContext_LocalizedString() {
// we specify that when the "getString" method of the mocked context is called, we want it to return the FAKE_STRING
when(context.getString(R.string.app_name)).thenReturn(FAKE_STRING);
ClassUnderTest myObjectUnderTest = new ClassUnderTest(context);
String result = myObjectUnderTest.getStringToTest();
assertThat(result, is(FAKE_STRING));
}
在他们的计算机上运行时,测试通过,没有问题。但是,当我尝试运行它时,我得到异常:
java.lang.RuntimeException: Stub!
at android.content.Context.getString(Context.java:14)
at examples.MockingObjectsExampleTest.readStringFromContext_LocalizedString(MockingObjectsExampleTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
错误指向以下行:
when(context.getString(R.string.app_name)).thenReturn(FAKE_STRING);
我已经尝试了我在网上能找到的所有东西,但无济于事。另外,我很困惑的是,另一位同事在单元测试中遇到了其他一些问题,将他的 Android Studio 升级到最新版本(我已经有的 2.1.1),"做了一些事情",现在它对他有用。当然,他不记得自己做了什么。是我应该更改的其他文件/配置吗?我已经有了build.gradle依赖项,就像其他人一样。谢谢。
我已经以艰难的方式修复了它:通过删除Android Studio以及所有相关文件和文件夹,然后重新安装它。现在,它按预期工作。