Search code examples
androidandroid-espressoandroid-architecture-componentsandroidx

How to run android instrumental test using `androidx` and `navgation architecture`?


I'm trying to write tests for an android app. Which has one Activity and two fragments (i.e FragmentOne and FragmentTwo) by clicking a button on FragmentOne it goes to the FragmentTwo. And I wrote the test for the same and I'm using androidx and navigation architecture. I have read this link and this link but didn't succeed.

Here is my current code-

ExampleInstrumentedTest

@RunWith(JUnit4::class)
class ExampleInstrumentedTest {

    @Test
    fun checkSecondFrag() {
        // Create a mock NavController
        val mockNavController = mock(NavController::class.java)

        // Create a graphical FragmentScenario for the OneFragment
        val oneFragmentScenario = launchFragmentInContainer<OneFragment>()

        // Set the NavController property on the fragment
        oneFragmentScenario.onFragment { fragment ->
            Navigation.setViewNavController(fragment.requireView(), mockNavController)
        }

        // Verify that performing a click prompts the correct Navigation action
        onView(ViewMatchers.withId(R.id.nextFragment)).perform(ViewActions.click())
        verify(mockNavController).navigate(R.id.action_oneFragment_to_twoFragment)
    }
}

Gradle(App)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.varun.navtesting"
        minSdkVersion 16
        targetSdkVersion 28
        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.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // Core library
    androidTestImplementation 'androidx.test:core:1.2.0'

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'

    // Assertions
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.ext:truth:1.2.0'
    androidTestImplementation 'com.google.truth:truth:0.42'

    // Espresso dependencies
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0'
    androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'


    testImplementation 'junit:junit:4.12'

    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'

    // required if you want to use Mockito for unit tests
    testImplementation 'org.mockito:mockito-core:2.25.0'
    // required if you want to use Mockito for Android tests
    androidTestImplementation 'org.mockito:mockito-android:2.19.0'

    // Fragment test
    implementation 'androidx.fragment:fragment:1.2.0-alpha01'
    androidTestImplementation 'androidx.fragment:fragment-testing:1.2.0-alpha01'

    implementation 'com.android.support:support-annotations:28.0.0'
}

Mainfiest

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.varun.navtesting">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <uses-library android:name="android.test.runner"
                      android:required="false"/>

        <uses-library android:name="android.test.base"
                      android:required="false"/>
        <uses-library android:name="android.test.mock"
                      android:required="false"/>
    </application>
    <instrumentation android:targetPackage="com.varun.navtesting"
                     android:name="android.test.ExampleInstrumentedTest"/>
</manifest>

This line

<instrumentation 
          android:targetPackage="com.varun.navtesting"
          android:name="android.test.ExampleInstrumentedTest"/>

is showing in red color in android studio.

Here is the Logcat

2019-07-30 22:00:16.967 6821-6837/com.varun.navtesting E/TestRunner: failed: checkSecondFrag(com.varun.navtesting.ExampleInstrumentedTest)
2019-07-30 22:00:16.967 6821-6837/com.varun.navtesting E/TestRunner: ----- begin exception -----
2019-07-30 22:00:16.968 6821-6837/com.varun.navtesting E/TestRunner: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.varun.navtesting/androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity (has extras) }
        at androidx.test.core.app.InstrumentationActivityInvoker.startActivity(InstrumentationActivityInvoker.java:344)
        at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:231)
        at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:209)
        at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:299)
        at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:282)
        at com.varun.navtesting.ExampleInstrumentedTest.checkSecondFrag(ExampleInstrumentedTest.kt:39)
        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:2145)
2019-07-30 22:00:16.968 6821-6837/com.varun.navtesting E/TestRunner: ----- end exception -----

Any help will be appriciated.


Solution

  • As per the Fragment testing page, you must use debugImplementation for the fragment-testing artifact:

    debugImplementation 'androidx.fragment:fragment-testing:1.2.0-alpha01'