我正在尝试使用androidx测试库的新FragmentScenario API,但是该测试无法尝试像这样启动片段场景:
fragmentScenario = launchFragmentInContainer<MyFragment>(args)
堆栈跟踪为:
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类(包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"
}
答案 0 :(得分:5)
您的应用程序需要通过fragment:1.1.0-alpha01
依赖关系依赖于implementation
-legacy-support-v4-:1.0.0
和appcompat:1.0.0
仅依赖于fragment:1.0.0
:
implementation "androidx.fragment:fragment:1.1.0-alpha01"