Espresso activity 开始失败并出现 NoSuchMethodError

Espresso activity start failing with NoSuchMethodError

我正在尝试为以下场景编写一个简单的测试:

在我的主 activity 上,我有一个按钮(带有 id validationButton),当用户单击它时会启动一个新的 activity(特定的 ValidationActivity)。所以我想测试当用户单击按钮时验证 activity 将开始。我的代码是这样的:

@RunWith(AndroidJUnit4::class)
@Config(sdk = [Build.VERSION_CODES.P])
    class MainActivityTest {
    
    @get:Rule
    val activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

    @Before
    fun intentsInit() {
        launch(MainActivity::class.java)
        Intents.init()
    }

    @After
    fun intentsTearDown() {
        Intents.release();
    }

    @Test
    fun click_start_validation_should_go_to_validation_screen() {
        val mockContext = mock(Context::class.java)
        //val scenario = launch(MainActivity::class.java)
        onView(withId(R.id.validationButton)).perform(click())
        assertThat(getIntents().first()).hasComponentClass(ValidationActivity::class.java)
    }
}

当我 运行 它时,测试失败并显示这些日志:

java.lang.NoSuchMethodError: androidx.test.internal.platform.util.TestOutputEmitter.addOutputProperties(Ljava/util/Map;)Z

at androidx.test.espresso.GraphHolder.baseLayer(GraphHolder.java:12)
at androidx.test.espresso.Espresso.<clinit>(Espresso.java:2)
at mobile.personaldemo.MainActivityTest.click_start_validation_should_go_to_validation_screen(MainActivityTest.kt:54)
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.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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.ExternalResource.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=12=](SandboxTestRunner.java:263)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=12=](Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

我错过了什么?任何帮助将不胜感激。

您是否尝试将 Thread.sleep(time: Milis) 放在 onView(withId(R.id.validationButton)).perform(click()) 之后? 也许 activity 没有完全加载,espresso 正在尝试验证它,但未通过测试。

或者,您可以使用您正在打开的 activity 中的一些 UI 组件,例如 TextView 或任何东西,并断言给定的 UI 组件 activity 显示。