我试图编写一个非常简单的检测测试用例,其代码可以在UI线程上运行。这是测试类:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml", sdk = 18)
public class GetTimeFromServerTest extends InstrumentationTestCase {
Context context = RuntimeEnvironment.application;
private MainActivity activity;
private Instrumentation instr;
@Before
public void setUp() throws Exception {
activity = Robolectric.buildActivity(MainActivity.class).create().get();
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
instr = getInstrumentation();
}
@Test
public void someTest() throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
doSomething();
}
});
}
}
但是当我尝试调用injectInstrumentation(InstrumentationRegistry.getInstrumentation())
时,我得到了
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at com.m1_mm.tools.GetTimeFromServerTest.setUp(GetTimeFromServerTest.java:53)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
如何运行此测试?