【问题标题】:AndroidJUnit4 test not found未找到 AndroidJUnit4 测试
【发布时间】:2016-09-02 09:22:08
【问题描述】:

我在这里注释了我的课程

@RunWith(AndroidJUnit4.class)
public class WorkdayProviderTest

此外,还像这样注释了我的测试方法

@Test
public void insert_dataInsertsCorrectly()

最后,像这样配置我的 build.gradle defaultConfig 和依赖项

defaultConfig {
    applicationId 'com.jueggs.workinghours'
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

androidTestCompile "junit:junit:4.12"
androidTestCompile "com.android.support:support-annotations:23.3.0"
androidTestCompile "com.android.support.test:runner:0.4.1"
androidTestCompile "com.android.support.test:rules:0.4.1"
androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.1"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:2.2.1"
androidTestCompile "org.hamcrest:hamcrest-library:1.3"

并像这样配置测试运行

它告诉我

No tests were found

Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

如何解决这个问题?

【问题讨论】:

    标签: android android-testing android-junit


    【解决方案1】:

    您的 Gradle 依赖项中可能没有某些内容。您是否正确声明了com.android.support.test:runner(可选com.android.support.test:rules)?

    请看一下我的build.gradle 文件:

    apply plugin: 'com.android.application'
    apply plugin: 'android-apt'
    
    buildscript {
        repositories {
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'
        }
    }
    
    apt {
        arguments {
            androidManifestFile variant.outputs[0].processResources.manifestFile
            resourcePackageName "com.piotr.testexample"
        }
    }
    
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
    
        useLibrary 'org.apache.http.legacy'
    
        //For building with Travis CI
        lintOptions {
            abortOnError false
        }
    
        packagingOptions {
            exclude 'META-INF/services/javax.annotation.processing.Processor'
        }
    
        defaultConfig {
            applicationId "com.piotr.testexample"
            minSdkVersion 16
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        ext.JUNIT_VERSION = '4.12'
        ext.AA_VERSION = '4.0.0'
        ext.SUPPORT_VERSION = '23.3.0'
        ext.ESPRESSO_VERSION = '2.2.2'
    
        apt "org.androidannotations:androidannotations:$AA_VERSION"
        compile "org.androidannotations:androidannotations-api:$AA_VERSION"
    
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile "junit:junit:$JUNIT_VERSION"
        testCompile("org.robolectric:robolectric:3.0") {
            exclude module: 'classworlds'
            exclude module: 'commons-logging'
            exclude module: 'httpclient'
            exclude module: 'maven-artifact'
            exclude module: 'maven-artifact-manager'
            exclude module: 'maven-error-diagnostics'
            exclude module: 'maven-model'
            exclude module: 'maven-project'
            exclude module: 'maven-settings'
            exclude module: 'plexus-container-default'
            exclude module: 'plexus-interpolation'
            exclude module: 'plexus-utils'
            exclude module: 'wagon-file'
            exclude module: 'wagon-http-lightweight'
            exclude module: 'wagon-provider-api'
        }
    
        compile "com.android.support:appcompat-v7:$SUPPORT_VERSION"
        compile "com.android.support:design:$SUPPORT_VERSION"
        compile "com.android.support:cardview-v7:$SUPPORT_VERSION"
        compile "com.android.support:recyclerview-v7:$SUPPORT_VERSION"
    
        compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
    
        compile 'com.squareup.retrofit:retrofit:1.9.0'
        compile 'com.google.code.gson:gson:2.4'
        compile 'com.squareup.picasso:picasso:2.5.2'
    
        androidTestCompile "com.android.support:support-annotations:$SUPPORT_VERSION"
        androidTestCompile "com.android.support.test.espresso:espresso-core:$ESPRESSO_VERSION"
        androidTestCompile 'com.android.support.test:runner:0.5'
        androidTestCompile "com.android.support.test.espresso:espresso-intents:$ESPRESSO_VERSION"
        /**
         * AccessibilityChecks
         * CountingIdlingResource
         * DrawerActions
         * DrawerMatchers
         * PickerActions (Time and Date picker)
         * RecyclerViewActions
         */
        androidTestCompile("com.android.support.test.espresso:espresso-contrib:$ESPRESSO_VERSION") {
            exclude group: 'com.android.support', module: 'appcompat'
            exclude group: 'com.android.support', module: 'support-v4'
            exclude group: 'com.android.support', module: 'support-v7'
            exclude group: 'com.android.support', module: 'design'
            exclude module: 'support-annotations'
            exclude module: 'recyclerview-v7'
        }
    
        compile "junit:junit:${JUNIT_VERSION}"
    }
    

    编辑:问题在于注释,请执行以下解决方案之一:

    • 删除@RunWith(AndroidJUnit4.class)
    • @RunWith(AndroidJUnit4.class) 更改为@RunWith(JUnit4.class)

    测试包中好像没有AndroidJUnit4.class了。

    【讨论】:

    • 删除@RunWith(AndroidJUnit4.class) 并运行测试。您还可以将@RunWith(AndroidJUnit4.class) 更改为@RunWith(JUnit4.class),它应该会运行。
    • 没有解决问题,在这两种情况下都找不到任何测试。在 0.4.1 版本中存在 AndroidJUnit4 类
    • 将 espresso 更新为 2.2.2 和 test-runner, test-rules 到 0.5 并删除注释。还有问题吗?
    • @RunWith(JUnit4.class) 为我工作
    【解决方案2】:

    尝试以这种方式命名您的类和方法:

    • Test_MyClass
    • test_myMethod

    我希望这会有所帮助。

    【讨论】:

    • 对不起。我通过 Espresso Instrumentation 测试以这种方式解决了这个问题。无论如何,在单元测试中我使用 Robolectric。如果您觉得卡住了,请尝试一下!
    • 在 android studio 中,在 Build Variants 中,你有没有从仪器测试切换到单元测试?
    【解决方案3】:

    好的,这个问题与我的测试配置无关,但是我之前删除的另一个类丢失了。该应用程序本身甚至没有启动。可惜没有提到类名

    【讨论】:

      【解决方案4】:

      确保您在 debug 上设置 buildType,如:https://stackoverflow.com/a/37371171/5175944 中所述

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-20
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-13
        相关资源
        最近更新 更多