Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

launchFragmentInContainer unable to resolve Activity in Android

While writing a simple test which uses launchFragmentInContainer, I get the following error message:

java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.myapp.appname.debug/androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity (has extras) }

The basic test class is:

class OneFragmentTest {

    @Test
    fun testOneFragmentState_checkTitleText() {
        val args = Bundle().apply {
            putString("dummyKey", "dummyValue")
        }
        launchFragmentInContainer<OneFragment>(args)

        onView(withId(R.id.tv_title)).check(matches(withText("title here")))
    }
}

I have tried to update AndroidManifest.xml with the following:

<instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.myapp.appname" />

but it seems that the tag instrumentation is valid but the values are written in red, so I assume something is wrong with the targetPackage and name.

How can I get rid of this error and run a simple test on OneFragment using launchFragmentInContainer?

like image 958
Bugdr0id Avatar asked Jun 12 '19 09:06

Bugdr0id


3 Answers

Try to configure this way

debugImplementation('androidx.fragment:fragment-testing:1.1.0') {
        // exclude androidx.test:core while fragment_testing depends on 1.1.0
        exclude group: 'androidx.test', module: 'core'
    }
like image 31
Levon Petrosyan Avatar answered Nov 12 '22 19:11

Levon Petrosyan


The error was related to the way I did import the dependencies on Gradle.

Before:

androidTestImplementation("androidx.fragment:fragment-testing:1.1.0-beta01")
implementation("androidx.fragment:fragment-ktx:1.1.0-beta01")
androidTestImplementation("androidx.test:core:1.2.0")
androidTestImplementation("androidx.test:rules:1.2.0")
androidTestImplementation("androidx.test:runner:1.2.0")

After:

debugImplementation("androidx.fragment:fragment-testing:1.1.0-beta01")
debugImplementation("androidx.fragment:fragment-ktx:1.1.0-beta01")
debugImplementation("androidx.test:core:1.2.0")
debugImplementation("androidx.test:rules:1.2.0")
debugImplementation("androidx.test:runner:1.2.0")

Changed from androidTestImplementation to debugImplementation and it solved the issue. Compiling and running, and green test as result.

like image 128
Bugdr0id Avatar answered Nov 12 '22 21:11

Bugdr0id


it works for me

def fragmentx_version = "1.1.0"
implementation "androidx.fragment:fragment:$fragmentx_version"
debugImplementation ("androidx.fragment:fragment-testing:$fragmentx_version"){
     exclude group: 'androidx.test', module: 'core'
}
debugImplementation 'androidx.test:core-ktx:1.2.0'

@Test
fun verifyMap() {
     FragmentScenario.launchInContainer(HomeFragment::class.java)

     onView(withId(R.id.map)).check(matches(isDisplayed()))
}

like image 3
Randriambolaniaina Edena Avatar answered Nov 12 '22 20:11

Randriambolaniaina Edena