NoSuchMethodError for FragmentManager.getFragmentF

2020-04-16 16:58发布

问题:

I'm trying to use the new FragmentScenario APIs of the androidx testing libraries but the test fails trying to launch the fragment scenario like so:

fragmentScenario = launchFragmentInContainer<MyFragment>(args)

The stacktrace is:

java.lang.NoSuchMethodError: androidx.fragment.app.FragmentManager.getFragmentFactory()Landroidx/fragment/app/FragmentFactory;

    at androidx.fragment.app.testing.FragmentScenario$1.perform(FragmentScenario.java:178)
    at androidx.fragment.app.testing.FragmentScenario$1.perform(FragmentScenario.java:171)
    at androidx.test.core.app.ActivityScenario.lambda$onActivity$0$ActivityScenario(ActivityScenario.java:478)
    at androidx.test.core.app.ActivityScenario$$Lambda$0.run(Unknown Source)
    at org.robolectric.android.fakes.RoboMonitoringInstrumentation.runOnMainSync(RoboMonitoringInstrumentation.java:53)
    at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:471)
    at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:170)
    at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:160)
    at com.domain.myapp.ui.pck.pkg.MyFragmentTest.setup(MyFragmentTest.kt:656)
    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.RunBefores.evaluate(RunBefores.java:24)

If I go the FragmentManager class (androidx.fragment.app FragmentManager) there is no getFragmentFactory method and the FragmentScenario.java class (package androidx.fragment.app.testing) clearly shows that the method does not exist:

I'm guessing my dependencies are configured incorrectly, this is my app/build.gradle file:

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.domain.app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode appVersionCode
        versionName appVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }
    aaptOptions {
        additionalParameters "--no-version-vectors"
    }
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        abortOnError false
    }
    testOptions {
        unitTests {
            includeAndroidResources true
            returnDefaultValues true
        }
    }
}

dependencies {
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation "androidx.appcompat:appcompat:1.0.0"

    testImplementation "junit:junit:4.12"
    testImplementation "org.mockito:mockito-core:2.7.1"
    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1"
    testImplementation "androidx.test:core:1.0.0"
    testImplementation "org.robolectric:robolectric:4.0.2"
    testImplementation "org.robolectric:shadows-support-v4:3.0"
    testImplementation "androidx.fragment:fragment-testing:1.1.0-alpha01"
    testImplementation 'androidx.test:runner:1.1.0'
    testImplementation 'androidx.test:rules:1.1.0'
    testImplementation "androidx.test.ext:junit:1.0.0"
    testImplementation "androidx.test.espresso:espresso-intents:3.1.0"
    testImplementation "androidx.test.espresso:espresso-core:3.1.0"
    testImplementation "androidx.test.ext:truth:1.0.0"
}

回答1:

Your application needs to depend on fragment:1.1.0-alpha01 via an implementation dependency - legacy-support-v4-:1.0.0 and appcompat:1.0.0 only depend on fragment:1.0.0:

implementation "androidx.fragment:fragment:1.1.0-alpha01"