gpt4 book ai didi

android - Espresso Intent 测试失败

转载 作者:IT老高 更新时间:2023-10-28 22:04:16 24 4
gpt4 key购买 nike

我正在学习使用 espresso 进行 android 仪器测试。我有一个应用程序,它有一个抽屉菜单,还有一个名为 About 的菜单。我正在测试单击该菜单项和 Activity 内容。

测试功能:

  @Test
public void testNavigationDrawerAboutMenu() {
onView(withId(R.id.drawer_layout))
.perform(DrawerActions.open()); //open drawer
onView(withText("About")).perform(click());
onView(withId(R.id.aboutsptemail)).check(matches(withText(R.string.screen_about_support_email)));
onView(withId(R.id.aboutcpright)).check(matches(isDisplayed()));
onView(withId(R.id.aboutprivacy)).check(matches(isDisplayed()));
onView(withId(R.id.abouttermsconditions)).check(matches(isDisplayed()));
onView(withId(R.id.aboutsptemail)).perform(click());
}

现在最后一个 TextView 中嵌入了网络链接。因此,当您单击它时,它会在应用程序的 Web View 中打开链接 (www.support.com)。我想测试这个功能。所以我尝试了这个:

intended(hasComponent(WebViewActivity.class.getName())); //check if webview called on supportEmail link click

但测试失败并出现此错误跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference
at android.support.test.espresso.intent.Intents.intending(Intents.java:155)
at com.ScanBuy.SmartLabel.NavigationDrawerActivityTests.testNavigationDrawerAboutMenu(NavigationDrawerActivityTests.java:94)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
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.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:1879)

我还尝试通过在检查 Intent 之前闲置资源来解决问题。但是没有用。有人可以帮忙吗?

最佳答案

我遇到了同样的问题,并通过使用 IntentsTestRule 而不是 ActivityTestRule 解决了它。 IntentsTestRuleActivityTestRule 的子类。设置您的 @Rule 来创建 Activity ,如下所示:

@Rule
public IntentsTestRule<MyActivity> mActivity = new IntentsTestRule<MyActivity>(MyActivity.class) {
@Override
protected Intent getActivityIntent() {
...
}
};

See the following project for more information: https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample

关于android - Espresso Intent 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37869418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com