I'm trying to use the new FragmentScenario APIs of the androidx testing libraries for local testing and instrumentation testing(androidTest). The api works fine in local environment but in instrumental testing, it gives error: java.lang.AssertionError: Activity never becomes requested state "[RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")"
Help me in instumental testing(androidTest)
Please check complete error detail:
java.lang.AssertionError: Activity never becomes requested state "[RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")
at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:228)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:198)
at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:169)
at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:160)
at com.techzis.avatr.LoginFragmentTest1.dummyTest(LoginFragmentTest1.kt:26)
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 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$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 androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2152)
Instrument Testing(androidTest) code is:
@RunWith(AndroidJUnit4::class)
class LoginFragmentTest1 {
@Test
fun dummyTest() {
val scenario = launchFragmentInContainer<LoginFragment>()
onView(ViewMatchers.withId(R.id.user_name)).perform(ViewActions.typeText("Hello World!"))
onView(ViewMatchers.withId(R.id.user_name)).check(matches(withText("Hello World!")))
}
}
Local unit test code is:
@RunWith(AndroidJUnit4::class)
@Config(application = MyApplication::class, shadows = [ShadowAndroidXMultiDex::class])
class LoginFragmentTest2 {
@Test
fun dummyTest() {
val scenario = launchFragmentInContainer<LoginFragment>()
onView(ViewMatchers.withId(R.id.user_name)).perform(ViewActions.typeText("Hello World!"))
onView(ViewMatchers.withId(R.id.user_name)).check(matches(withText("Hello World!")))
}
}
And app level build.gradle file is :
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs"
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
multiDexEnabled false
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
androidExtensions {
experimental = true
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
kapt {
javacOptions {
option("-Xmaxerrs", 1000)
}
}
testOptions {
unitTests.includeAndroidResources = true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
configurations.all {
resolutionStrategy {
force 'com.google.code.findbugs:jsr305:3.0.2'
force 'org.jetbrains.kotlin:kotlin-reflect:1.2.71'
}
}
sourceSets {
test { java.srcDirs += "$projectDir/src/testShared" }
androidTest {
java.srcDirs += "$projectDir/src/testShared"
resources.srcDirs += "$projectDir/src/test/resources"
}
}
}
dependencies {
def lifecycle_version = "2.0.0"
def fragment_version = "1.1.0-alpha01"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation "androidx.fragment:fragment:$fragment_version"
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.4.0'
implementation "com.squareup.moshi:moshi-kotlin:1.8.0"
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.8.0'
kapt "com.android.databinding:compiler:$gradle_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation 'com.github.florent37:diagonallayout:1.1.1'
testImplementation 'androidx.test:core:1.0.0'
testImplementation 'org.robolectric:robolectric:4.1-alpha-1'
androidTestImplementation 'androidx.test:runner:1.1.0'
testImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
testImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
testImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
testImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
testImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
testImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation "io.mockk:mockk-android:1.8.13.kotlin13"
testImplementation "io.mockk:mockk:1.8.13.kotlin13"
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version" // Test helpers for navigation
androidTestImplementation "androidx.fragment:fragment-testing:$fragment_version"
testImplementation "androidx.fragment:fragment-testing:$fragment_version"
androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version" // Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version" // Test helpers for LiveData
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.10.0'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.10.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'
}