尝试将 AndroidJUnit4 与标准 JUnit 运行器和委托运行器 'org.robolectric.Robolectric.RobolectricTestRunner' 一起使用时无法加载

我正在运行一些测试,但我不知道为什么机器人电包含在此错误中,因为测试仅适用于浓缩咖啡,下面是错误:espresso

java.lang.Exception: Attempted to use AndroidJUnit4 with standard JUnit runner and delegate runner 'org.robolectric.RobolectricTestRunner'could not be loaded. Check your build configuration.
at android.support.test.runner.AndroidJUnit4.loadRunner(AndroidJUnit4.java:89)
at android.support.test.runner.AndroidJUnit4.loadRunner(AndroidJUnit4.java:63)
at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:57)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at androidx.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:63)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at androidx.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:153)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:73)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:789)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:543)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:386)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)

我按照浓缩咖啡的文档,我看到一切都很好。可能是什么问题:

以下是我的 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'


jacoco {
    toolVersion = "0.7.6.201602180812"
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def debugTree = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "$project.projectDir/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: project.buildDir, includes: [
            'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
    ])
}


android {

    adbOptions {
        timeOutInMs 60 * 1000
    }

    compileSdkVersion 28
    defaultConfig {
        applicationId "com.levelapp.converge.convergelevelapp"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            testCoverageEnabled true
        }

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    testOptions {
        animationsDisabled true

        unitTests {
            includeAndroidResources = true
        }

        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:animated-vector-drawable:28.0.0'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    androidTestUtil 'com.android.support.test:orchestrator:1.0.2-alpha1'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:3.7.1'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.koushikdutta.ion:ion:2.+'
    implementation 'com.mikhaellopez:circularimageview:3.2.0'
    implementation 'com.android.support:multidex:1.0.3'


    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'


    androidTestImplementation('com.android.support:multidex-instrumentation:1.0.1') {
        exclude group: 'com.android.support', module: 'multidex'
    }

}

然后下面是正在运行的测试:

@Rule
public ActivityTestRule<MainActivity> activity = new ActivityTestRule<>(MainActivity.class);

@Test
public void loadRecyclerview(){

    Espresso.onView(ViewMatchers.withId(R.id.rv)).
            perform(RecyclerViewActions.actionOnItemAtPosition(0, ViewActions.click()));

    Espresso.onView(withId(R.id.text))
            .check(matches(withText("k33too")))
            .check(matches(isDisplayed()));
}

可能是什么问题?


答案 1

我看到你正在使用AndroidX。确保使用正确的导入。我今天遇到了这个确切的问题,并意识到我仍然在使用旧的,而我应该使用import android.support.test.runner.AndroidJUnit4;import androidx.test.runner.AndroidJUnit4;

这为我修复了错误。


答案 2

更新 kotlin。确保所有注释都采用 - 这在我的情况下是一个问题。@Rule@get:Rule


推荐