我想使用Espresso
使用IdlingResource
来测试我的Android项目,根据ActivityTestRule,默认情况下每个Test方法应该启动一次Activity。但是,我想使用相同的活动测试案例。我尝试将launchActivity
设置为false,但结果是NullPointerException
。
@LargeTest
@RunWith(AndroidJUnit4.class)
public class VideoViewPlayingTest {
@Rule
public MainActivityTestRule mRule = new MainActivityTestRule(MainActivity.class, true, false);
private BaseIdleResource mResource;
@Before
public void setUp() {
mResource = new BaseIdleResource(mRule.getActivity());
Espresso.registerIdlingResources(mResource);
}
@After
public void tearDown() {
Espresso.unregisterIdlingResources(mResource);
}
@Test
public void controllerViewKeepShowing() {
onView(withId(R.id.videoView)).perform(click());
ViewInteraction linearLayout = onView(
withId(R.id.controller_view));
linearLayout.check(matches(isCompletelyDisplayed()));
}
@Test
public void playButtonShowWhenControllerViewShow() {
onView(withId(R.id.videoView)).perform(click());
ViewInteraction pauseButton = onView(
withId(R.id.pause));
pauseButton.check(matches(isCompletelyDisplayed()));
}
}
错误讯息:
java.lang.NullPointerException
at android.support.test.espresso.core.deps.guava.base.Preconditions.checkNotNull(Preconditions.java:210)
at android.support.test.espresso.core.deps.guava.collect.SingletonImmutableList.<init>(SingletonImmutableList.java:40)
at android.support.test.espresso.core.deps.guava.collect.ImmutableList.copyOf(ImmutableList.java:293)
at android.support.test.espresso.Espresso.unregisterIdlingResources(Espresso.java:127)
at com.hustunique.sample.VideoViewPlayingTest.tearDown(VideoViewPlayingTest.java:61)
at java.lang.reflect.Method.invoke(Native Method)
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.RunAfters.evaluate(RunAfters.java:33)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
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.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1985)
我该如何解决这个问题?