7

When implement code to test UI in android using kotlin.

this is my code

this is my code

I thinks it 's simple code to test but android studio prevent me due to some problem > I searched a lot for this but no result . I tried a lot of solution but no result also . Thanks .

I get this error and activity which I test doesn't start .

java.lang.AssertionError: Activity never becomes requested state "[STARTED, RESUMED, DESTROYED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")
at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:301)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:235)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:190)
at com.elgaml.myuitesting.MainActivityTest.test_isActivityinView(MainActivityTest.kt:18)
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 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:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2204)

and my build.gradle is :

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.elgaml.myuitesting"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'

    def androidx_test_espresso="3.2.0"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$androidx_test_espresso"
    implementation "androidx.test.espresso:espresso-idling-resource:$androidx_test_espresso"


    // Mockk.io
    def mockk_version = "1.9.3"
    androidTestImplementation "io.mockk:mockk-android:$mockk_version"

    // androidx.test
    def androidx_test = "1.1.0"
    androidTestImplementation "androidx.test:runner:$androidx_test"
    androidTestImplementation "androidx.test:core:$androidx_test"
    androidTestImplementation "androidx.test.ext:junit-ktx:$androidx_test"

}
Bashir
  • 2,057
  • 5
  • 19
  • 44
  • Remove the `moveToState` line and see if that helps. – gosr May 29 '20 at 16:38
  • still give me same error – Mahmoud Elgamal Jun 02 '20 at 00:18
  • Then it's not clear what causes the issue. You should post your code of MainActivity. Or you can try to create a new, empty activity and use that in your test instead (testing one random view in that new activity layout), and then you should see a passing test. – gosr Jun 02 '20 at 07:00
  • The test might be monitoring the activity on the wrong package. Try launching via actual intent and call setPackage(InstrumentationRegistry.getInstrumentation().context.packageName) on the intent you use to launch. – Ostkontentitan Jul 30 '21 at 06:53

2 Answers2

0

allow autostart and display pop-up windows for this app on your android phone settings.

Mahdi Zareei
  • 1,299
  • 11
  • 18
0

The problem was in the library I used. The new library is: Espresso.

def androidx_test_espresso = "3.1.0"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidx_test_espresso"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$androidx_test_espresso"
implementation "androidx.test.espresso:espresso-idling-resource:$androidx_test_espresso"
implementation "androidx.test.espresso:espresso-idling-resource:3.3.0"
flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25