我试图使用androidx测试库的新碎片场景api,但是测试未能启动碎片场景,如下所示:fragmentScenario = launchFragmentInContainer<MyFragment>(args)
stacktrace是:
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)
如果我转到fragmentmanager类(androidx.fragment.app fragmentmanager),则没有getfragmentfactory方法,fragmentscenario.java类(package androidx.fragment.app.testing)清楚地显示该方法不存在:
我猜我的依赖项配置不正确,这是我的app/build.gradle文件:
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"
}
最佳答案
您的应用程序需要通过依赖关系依赖于fragment:1.1.0-alpha01
,并且仅依赖于implementation
:
implementation "androidx.fragment:fragment:1.1.0-alpha01"
关于android - FragmentManager.getFragmentFactory的NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53442211/