Espresso意图测试失败

Hir*_*el_ 29 android android-intent android-testing android-espresso

我正在学习使用浓缩咖啡的android仪器测试.我有一个有抽屉菜单的应用程序,有一个叫做关于的菜单.我正在测试点击该菜单项和活动内容.

testfunction:

  @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());
}
Run Code Online (Sandbox Code Playgroud)

现在最后一个textview中嵌入了weblink.因此,当您点击它时,它会自动在应用程序的Web视图中打开链接(www.support.com).我想测试这个功能.所以我试过这个:

intended(hasComponent(WebViewActivity.class.getName())); //check if webview called on supportEmail link click
Run Code Online (Sandbox Code Playgroud)

但是此错误跟踪测试失败:

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)
Run Code Online (Sandbox Code Playgroud)

我还尝试通过在检查意图之前空闲资源来解决问题.但没有奏效.有人可以帮忙吗?

spl*_*tte 75

我有同样的问题,并通过使用IntentsTestRule而不是解决它ActivityTestRule.IntentsTestRule是.的子类ActivityTestRule.设置你@Rule创建活动,如下所示:

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

有关详细信息,请参阅以下项目:https: //github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample

  • 声明这个规则在内部调用`init()`和`release()`但如果你不想声明`IntentsTestRule`那么你必须在你的代码中明确地调用`Intents.init()`这将使你的代码运行没有任何错误. (3认同)

bon*_*nyz 8

如果您使用的是自定义ActivityTestRule,则可以添加正确的Intents.init(), Intents.release()调用:

@Override
protected void afterActivityLaunched() {
    Intents.init();
    super.afterActivityLaunched();
}

@Override
protected void afterActivityFinished() {
    super.afterActivityFinished();
    Intents.release();
}
Run Code Online (Sandbox Code Playgroud)

  • 其实是一样的。如果你已经有一个自定义的 ActivityTestRule 或者你想动态地启用这个功能,那么使用这种方法是有意义的,否则只需使用 IntentsTestRule。 (2认同)

Pet*_*son 8

我遇到了同样的问题,但是,切换到它IntentsTestRule也不起作用。因此,我在发送Intent的测试前后切换回ActivityTestRule并调用。 Intents.init()Intents.release()

有关更多信息,请参见此参考


小智 8

您应该使用推荐的方法,而不是使用java中IntentsTestRule现在的which :@deprecatedactivityScenarioRule

@RunWith(AndroidJUnit4::class)
@MediumTest
class YourActivityTest {

    @get:Rule
        val activityScenario = activityScenarioRule<YourActivity>()

    @Before
        fun setUp() {
            launchActivity<YourActivity>()
            Intents.init()
        }
    
        @After
        fun tearDown() {
            Intents.release()
        }

    @Test
        fun should_goBackTo_MainActivity_onBackButton_click() {

            onView(withId(R.id.goBackBtn)).perform(click())
            intended(hasComponent(hasShortClassName(".MainActivity")))
        }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将其添加到您的build.gradle文件中:

android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
// testing
androidTestImplementation 'androidx.test:core:1.3.1-alpha02'
androidTestImplementation 'androidx.test:core-ktx:'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'   
//for the activityScenarioRule syntax to work in kotlin,
//We add the ktx version of androidx.test.ext:junit
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
}
Run Code Online (Sandbox Code Playgroud)