I am try to write a simple test for the below scenario:
On my main activity I have a button (with id validationButton) and when users clicks on it starts a new activity (specific ValidationActivity). So I want to test that when user click the button validation activity is going to start. My code is this:
@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)
}
}
When i run it, test fails with these logs:
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$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 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$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(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)
What am I missing? Any help will be appreciated.
Did you tried to put Thread.sleep(time: Milis)
after onView(withId(R.id.validationButton)).perform(click())
? Maybe the activity didn't load fully and espresso is trying to verify it, and that fails the test.
Alternatively, you can use some UI component from the activity that you're opening, such as TextView or anything, and assert that the UI component from the given activity is shown.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.