Espresso测试不能用Junit5和multidex运行。

问题描述 投票:0回答:1

我们的espresso测试不再运行了,而以前用Junit5的时候却可以。在Android Studio 4上运行它们时,它们可以工作,但在gradle CLI上运行它们时,却不能使用 ./gradlew app:connectedDebugAndroidTest

他们目前失败与。

> 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的去ugaring
  • 测试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}"

}

{fnTahomafs10bord0shad01cH00FFFF}And 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)
    }
}
android android-espresso android-testing junit5
1个回答
0
投票

解决方法是明确使用kotlin的jdk8 stdlib。

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"
© www.soinside.com 2019 - 2024. All rights reserved.