launchFragmentInContainer unable to resolve Activity in Android

11,572

Solution 1

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.

Solution 2

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'
    }

Solution 3

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()))
}

Solution 4

I referred the test samples for Espresso and have used the following set of dependencies in app level build.gradle file

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:' + rootProject.androidxCompatVersion
implementation 'androidx.core:core-ktx:' + rootProject.androidxCoreVersion
implementation 'androidx.fragment:fragment-ktx:' + rootProject.androidxFragmentVersion

// Ideally this would only be present in test scope (Adding this fixed the issue for me)
debugImplementation 'androidx.fragment:fragment-testing:' + rootProject.androidxFragmentVersion
debugImplementation 'androidx.test:core:' + rootProject.coreVersion

testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
testImplementation 'androidx.test:core:' + rootProject.coreVersion
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
testAnnotationProcessor 'com.google.auto.service:auto-service:1.0-rc4'

androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
androidTestImplementation 'androidx.fragment:fragment-testing:' + rootProject.androidxFragmentVersion
androidTestImplementation 'org.robolectric:annotations:' + rootProject.robolectricVersion     }

Within you project level build.gradle file

ext {
buildToolsVersion = "28.0.3"
androidxCoreVersion = "1.1.0-rc02"
androidxCompatVersion = "1.1.0-rc01"
androidxFragmentVersion = "1.1.0-rc01"
coreVersion = "1.3.0-alpha03"
extJUnitVersion = "1.1.2-alpha03"
runnerVersion = "1.3.0-alpha03"
rulesVersion = "1.3.0-alpha03"
espressoVersion = "3.3.0-alpha03"
robolectricVersion = "4.3.1"   }

Inside the isolated fragment test class

@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class ExampleFragmentTest{
@Test
fun launchFragmentAndVerifyTheUI(){
   /* This is used to launch the fragment in an isolated environment ( This is essentially an empty activity that
   houses that fragment ) */
   launchFragmentInContainer<ExampleFragment>()
    // Now using Espresso to verify the fragment's UI, i.e textview and verifying that its getting displayed
    onView(withId(R.id.textView)).check(matches(withText("I am a fragment")))
}  }
Share:
11,572
Bugdr0id
Author by

Bugdr0id

SOreadytohelp

Updated on June 19, 2022

Comments

  • Bugdr0id
    Bugdr0id almost 2 years

    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?

  • Astha Garg
    Astha Garg almost 5 years
    It's good that you found solution, but developer.android.com/training/testing/set-up-project how come it's working for them?
  • bsautner
    bsautner over 4 years
    you can use debugImplementation so it doesn't get built in with your release builds
  • Karol Kulbaka
    Karol Kulbaka about 4 years
    set debugImplementation for fragment-testing should be enough, rest test dependencies can stay as `androidTestImplementation
  • amlwin
    amlwin over 3 years
    why do we need to change debugImplementation ?
  • Vít Kapitola
    Vít Kapitola over 3 years
    Setting DebugImplementation just fo fragment-testing is OK (as mentioned in Android docs)
  • IgorGanapolsky
    IgorGanapolsky over 3 years
    That makes no sense. All Espresso tests go in androidTestImplementation
  • IgorGanapolsky
    IgorGanapolsky over 3 years
    Why not androidTestImplementation?
  • Bita Mirshafiee
    Bita Mirshafiee over 2 years
    After changing androidTestImplementation to debugImplementation I got errors in my manifest that I don't have 'exported' field and it is needed in android 12 and above which I had it in my manifest and it was keep complaining, but this answer solved the problem.