android-test
android-test copied to clipboard
Got AssertionError while launching a activity by ActivityScenario.launch
Description
Got Error messages as below: java.lang.AssertionError: Activity never becomes requested state "[CREATED, DESTROYED, RESUMED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE") at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338)
some message shows in log E/TestRunner( 3422): ----- begin exception ----- E/TestRunner( 3422): java.lang.AssertionError: Activity never becomes requested state "[RESUMED, CREATED, DESTROYED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE") E/TestRunner( 3422): at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338) E/TestRunner( 3422): at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:272) E/TestRunner( 3422): at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226)
Steps to Reproduce
- assign a different process name to the test activity (not a launcher category) in AndroidManifest.xml

- run test code

- The activity UI shows on screen but can't be operated by onView and then it shows the error after a few seconds.
AndroidX Test and Android OS Versions
OS version: android 10 androidx.test.espresso core: 3.4.0 androidx.test.rule: 1.4.0
workaround solution
don't give android:process name to the test activity
me too
me too
@sayasheng @wangprofessor @biezhihua , could you provide a sample to reproduce this problem? It will help to validate and fix this problem.
@sayasheng @wangprofessor @biezhihua , could you provide a sample to reproduce this problem? It will help to validate and fix this problem.
https://github.com/biezhihua/ActivityScenarioTestDemo
I provide a demo project, you can reproduce this problem.
java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, CREATED, RESUMED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE") at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:339) at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:273) at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:196) at com.bzh.test.BZHTest.bzhtest(BZHTest.kt:13)
@utzcoz Have you made any progress?
@utzcoz Have you made any progress?
Sorry for that. Looks like Brett and other maintainers are busy recently. I will take look at this issue this week before they have time for it. And I will post updates to here.
@sayasheng @wangprofessor @biezhihua , could you provide a sample to reproduce this problem? It will help to validate and fix this problem.
https://github.com/biezhihua/ActivityScenarioTestDemo
I provide a demo project, you can reproduce this problem.
I can't run your sample at Android Studio. Could you provide one sample with basic Android Studio structure, and without alibaba's gitlab link that I can't access?
E/TestRunner( 3422): java.lang.AssertionError: Activity never becomes requested state "[RESUMED, CREATED, DESTROYED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE")
Okay, I reproduce this problem with my local sample app. It will raise when add process to test Activity's AndroidManifest.xml tag.
Looks like androidx.test core uses MonitoringInstrumentation to receive Activity state from instrumentation, and it doesn't receive state correctly state from Instrumentation when started Activity with specific process.
Found a sample to test multiprocess: https://github.com/android/android-test/tree/9d25d24ec7abe5021672c0423a992e87b44eb261/testapps/multiprocess_testapp. Maybe you can check it whether it can help you.
Found a sample to test multiprocess: https://github.com/android/android-test/tree/9d25d24ec7abe5021672c0423a992e87b44eb261/testapps/multiprocess_testapp. Maybe you can check it whether it can help you.
It looks like related to Robolectric tests. Hi @brettchabot , does ActivityScenario support multiprocess correctly on emulator?
Found another tutorial to test app with multi-process with espresso remote: https://developer.android.com/training/testing/espresso/multiprocess.
Found another tutorial to test app with multi-process with espresso remote: https://developer.android.com/training/testing/espresso/multiprocess.
From testing result, we can start an Activity in default process with ActivityScenario, and start another Activity with custom private process, and Espresso can work correctly with multi-processes based on espresso-remote library. But the problem of using ActivityScenario to start Activity with custom private process directly also exists. One dirty workaround to fix this problem is to add a dummy Activity for androidTest to start Activity with custom private process config, and use ActivityScenario to setup this dummy Activity and use it to trigger another Activity will be started into private process. Also we need to follow above mentioned tutorial to setup Espresso remote for androidTest. After that, we can use Espresso to test Activity UI in private process.
E/TestRunner( 3422): java.lang.AssertionError: Activity never becomes requested state "[RESUMED, CREATED, DESTROYED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE")
Okay, I reproduce this problem with my local sample app. It will raise when add
processto test Activity'sAndroidManifest.xmltag.
Thank your work!
It will raise when not add process to test Activity's AndroidManifest.xml tag.
You can try it again.
I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem without process tag.

You will get same information.
@utzcoz
@biezhihua I will check your new sample this weekend.
I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem without process tag.
You will get same information.
You didn't add your test Activity to your AndroidManifest.xml, so system can't find your test Activity, and raised this error to you.
I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem without process tag.
You will get same information.
You didn't add your test Activity to your AndroidManifest.xml, so system can't find your test Activity, and raised this error to you.
Sorry, this is my error, i forget to add Activity to my AndroidManifest.xml.
I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem.
@biezhihua There are some things about your sample:
- You should add
AndroidJUnit4as yourTestCase's test runner. - You should avoid add
TestActivitytoAndroidManifest.xmlunderandroidTestandtestdirectry. From https://issuetracker.google.com/issues/127986458#comment6 we know AGP doesn't mergeAndroidManifest.xmlunderandroidTestandtest, you definition inandroidTest'sAndroidManifest.xmldoesn't work, and yourTestActivitywill not found and started by test case, and you will encounter above problem. If you really want to add pureTestActivity, you can follow https://github.com/robolectric/robolectric/pull/4736 to trytestappworkaround from Robolectric.
@biezhihua There are some things about your sample:
- You should add
AndroidJUnit4as yourTestCase's test runner.- You should avoid add
TestActivitytoAndroidManifest.xmlunderandroidTestandtestdirectry. From https://issuetracker.google.com/issues/127986458#comment6 we know AGP doesn't mergeAndroidManifest.xmlunderandroidTestandtest, you definition inandroidTest'sAndroidManifest.xmldoesn't work, and yourTestActivitywill not found and started by test case, and you will encounter above problem. If you really want to add pureTestActivity, you can follow Workaround for AGP not merging test manifest robolectric/robolectric#4736 to trytestappworkaround from Robolectric.
Thank you very much.
Sorry for delayed response. ActivityScenario won't support testing an Activity running in a different process than the Instrumentation. At the very least I feel ActivityScenario should detect and report a better error message in this situation.
Does it help if you specify
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.example.activityscenariotestdemo"
android:targetProcesses=":welcome"
/>
in your test's AndroidManifest.xml?
@brettchabot in your previous comment did you forget the backticks? because it looks like there might be XML inserted mid-sentence.
I could get it to show by pressing "reply" 🤓:
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.example.activityscenariotestdemo"
android:targetProcesses=":welcome"
/>
oops thanks
I ran into this today for a Jetpack Compose project. I am able to work around this issue by removing testCoverageEnabled true of android.buildTypes.debug. Unfortunately, I need that. :(