I'm trying to write a simple UI test for my Android app:
@RunWith(AndroidJUnit4::class)
class SimpleTest {
@get:Rule
val activityRule = activityScenarioRule<MainActivity>()
@Test
fun justPass() {}
}
The problem is that this test hangs forever, and the app gets stuck at this screen:
It happens both on an emulator and on a real device. The complete title is androidx.test.core.app.InstrumentationActivityInvoker$EmptyActivity.
Strangely, the test completes if I press the square button and then select the same screen from the recent apps.
These are my test dependencies:
testImplementation 'junit:junit:4.13.1'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation 'android.arch.core:core-testing:2.1.0'
testImplementation "io.mockk:mockk:$mockk_version"
androidTestImplementation 'androidx.test:core-ktx:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
If I add androidx.test:rules:1.3.0 and replace activityScenarioRule with ActivityTestRule, the test completes automatically as I expected. But ActivityTestRule has been deprecated, so I don't think that's a good option.
So what should I do to make this test pass without getting stuck?
Update
MainActivity is declared in the manifest with launch mode as single instance:
<activity android:name=".MainActivity"
android:launchMode="singleInstance">
...
</activity>
If I remove the launchMode attribute, the test no longer hangs. Still, it should be possible to test an activity that has that attribute, so I'm still looking for a solution.
