Espresso测试无法与Junit5和multidex一起运行。

3
我们的浓缩咖啡测试无法运行,尽管它们曾经在Junit5上运行。当从Android Studio 4运行它们时,它们可以正常工作,但是使用./gradlew app:connectedDebugAndroidTest命令行界面(CLI)时则无法工作。
目前出现以下错误:
> Task :app:connectedDebugAndroidTest
Starting 3 tests on Pixel_3a(AVD) - 10

com.bonial.tests.BrochureViewerShelfTrackingTest > initializationError[Pixel_3a(AVD) - 10] FAILED
        java.lang.NoSuchMethodError: No static method newKeySet()Lj$/util/concurrent/ConcurrentHashMap$KeySetView; in class Lj$/util/concurrent/ConcurrentHashMap; or its super classes (declaration of 'j$.util.concurrent.ConcurrentHashMap' appears in /data/app/com.bonial.kaufda.test-pEhGL5P8v0MAVR6s2R8e4A==/base.apk!classes4.dex)
        at org.junit.platform.commons.logging.LoggerFactory.<clinit>(LoggerFactory.java:36)

com.xxx.tests.OpenBrochureTrackingTest > initializationError[Pixel_3a(AVD) - 10] FAILED
        java.lang.IllegalStateException: junit-platform-runner not found on runtime classpath of instrumentation tests; please review your androidTest dependencies or raise an issue.
        at de.mannodermaus.junit5.AndroidJUnit5Builder.runnerForClass(RunnerBuilder.kt:72)

com.xxx.tests.OpenBrochureWithOnboardingTrackingTest > initializationError[Pixel_3a(AVD) - 10] FAILED
        java.lang.IllegalStateException: junit-platform-runner not found on runtime classpath of instrumentation tests; please review your androidTest dependencies or raise an issue.
        at de.mannodermaus.junit5.AndroidJUnit5Builder.runnerForClass(RunnerBuilder.kt:72)

我检查了 classes4.dex,发现该方法不存在,但它在另一个 classes.dex 文件中。

可能是以下任何原因导致的:

  • 迁移到AGP 4.0
  • 启用Java 8解糖
  • 测试apk变得更大,因此dex文件分割方式不同

这是我们的设置:

app/build.gradle:

android {
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
        [...]
    }

    defaultConfig {
        testInstrumentationRunner "com.bonial.framework.utils.CustomEspressoTestRunner"
        testInstrumentationRunnerArgument "runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder"
        [...]
    }
    [...]
}

dependencies {
   [...]
   // Espresso
    androidTestImplementation "androidx.multidex:multidex:${versions.multidex}"
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation "androidx.test:runner:${versions.supportTestVersion}"
    androidTestImplementation "androidx.test:rules:${versions.supportTestVersion}"
    androidTestImplementation("com.schibsted.spain:barista:${versions.barista}") {
        exclude group: 'org.jetbrains.kotlin'
    }
    androidTestImplementation "com.beust:klaxon:${versions.klaxon}"
    androidTestImplementation "org.yaml:snakeyaml:${versions.snakeyaml}"
    androidTestImplementation "androidx.test.espresso:espresso-core:${versions.espresso}"
    implementation "androidx.test.espresso:espresso-idling-resource:${versions.espresso}"
    // To be able to honor espresso-contrib dependencies, our support library dependencies must match espresso's
    androidTestImplementation("androidx.test.espresso:espresso-contrib:${versions.espresso}") {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
        exclude module: 'recyclerview-v7'
    }
    androidTestImplementation("org.junit.platform:junit-platform-suite-api:${versions.junitPlatform}")
    androidTestImplementation("org.junit.platform:junit-platform-runner:${versions.junitPlatform}")
    androidTestImplementation("org.junit.jupiter:junit-jupiter:${versions.junit5}")
    androidTestImplementation "org.junit.jupiter:junit-jupiter-api:${versions.junit5}"
    androidTestImplementation "de.mannodermaus.junit5:android-test-core:${versions.junit5AndroidTest}"
    androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:${versions.junit5AndroidTest}"
    debugImplementation "androidx.fragment:fragment-testing:${versions.fragment}"
    androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito-inline:${versions.dexmaker}"
    androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:${versions.mockitoKotlin}"
    androidTestImplementation "org.amshove.kluent:kluent-android:${versions.kluent}"
    androidTestImplementation "com.jayway.jsonpath:json-path:${versions.jsonPath}"
    androidTestImplementation "com.squareup.rx.idler:rx2-idler:${versions.rxIdler}"
    androidTestImplementation "androidx.core:core:${versions.coreKtx}"

}

还有 CustomEspressoTestRunner.kt

@Suppress("unused")
class CustomEspressoTestRunner : AndroidJUnitRunner() {

    override fun onCreate(arguments: Bundle?) {
        MultiDex.install(getTargetContext())
        StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
        super.onCreate(arguments)
    }

    @Throws(InstantiationException::class, IllegalAccessException::class, ClassNotFoundException::class)
    override fun newApplication(cl: ClassLoader?, className: String?, context: Context?): Application? {
        return super.newApplication(cl, EspressoTestApplication::class.java.name, context)
    }
}

1
我也遇到了同样的问题,你解决了吗? - Shah
2个回答

1

对于遇到同样问题的其他人,将我的minSdk更新为大于或等于24的值可以解决此问题。


1
解决方案是明确使用Kotlin的JDK8 stdlib:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接