【问题标题】:Android Studio Error JUNIT4 !!! JUnit version 3.8 or later expected:Android Studio 错误 JUNIT4 !!!预计 JUnit 版本 3.8 或更高版本:
【发布时间】:2014-03-31 09:22:35
【问题描述】:

我正在做 Android 项目(在 Android Studio 中),里面有一个纯 Java 的小 SDK。

所以我想要从 JUnit4 进行测试。

而我是这样配置 Gradlle 文件的:

apply plugin: 'android'

android {
    compileSdkVersion "Google Inc.:Google APIs:19"
    buildToolsVersion "19.0.1"

    lintOptions{
        checkReleaseBuilds false
    }

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 28
        versionName "4.0.5"
    }

    signingConfigs {
        debug {
        }

        release {
        }

    }

    buildTypes {

        debug{
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            debuggable true
            buildConfigField "boolean", "LOG_ENABLED", "true"
            signingConfig signingConfigs.debug
        }

        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            debuggable false
            buildConfigField "boolean", "LOG_ENABLED", "false"
            signingConfig signingConfigs.release
        }
    }

    productFlavors{
        develFlavor{
        }

        testFlavor{
        }

        trainingFlavor{
        }

        preFlavor{
        }

        proFlavor{

        }
    }

}

if (project.hasProperty('storePassword')) {
    android.signingConfigs.release.storePassword = storePassword
}

if (project.hasProperty('keyAlias')) {
    android.signingConfigs.release.keyAlias = keyAlias
}

if (project.hasProperty('keyPassword')) {
    android.signingConfigs.release.keyPassword = keyPassword
}

//testing
sourceSets {
    unitTest {
        java.srcDir file('test')
        resources.srcDir file('test/resources')
    }
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}

build.dependsOn unitTest

dependencies {
    compile files('libs/junit-4.11-sources.jar')
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile files("$project.buildDir/classes/debug")
    compile 'com.google.code.gson:gson:+'
    compile 'com.android.support:appcompat-v7:+'
    compile files('libs/libGoogleAnalyticsServices.jar')
    compile files('libs/sbc_mapslib.jar')
    compile files('libs/t21c2dm-lib-v1.0.jar')
}

然后我做一个简单的测试用例来看看junitis是否有效。但是当我运行它时,会提示这个错误。

!!! JUnit version 3.8 or later expected:

我在其他一些问题中看到解决方案是,将 Junit4.jar 的依赖项更改为第一位,但它不起作用

【问题讨论】:

    标签: android android-studio junit4 android-gradle-plugin build.gradle


    【解决方案1】:

    如果您测试 Android 的特定行为,这意味着如果您需要在设备(硬件或模拟器)上运行测试,则不能使用 JUnit 4,因为它没有内置在该版本中。您需要使用 JUnit 3(我知道这很麻烦)。 如果您有一些不需要运行 Android 的测试,则可以使用您喜欢的每个版本(就像使用标准 JUnit 测试一样)。

    【讨论】:

    • 我不需要测试 Android 代码(暂时)。只需要测试连接到服务器并获取 JSON 的纯 Java 代码。在android studio中,我没有运行所有的项目,只是这个测试类要测试,然后就报错了。
    • Android 插件在非 Android 测试中表现不佳。您最好的选择可能是将纯 Java 代码分离成一个纯 Java 模块,该模块使用 Gradle 中的 java 插件构建。然后你可以随心所欲地测试它。
    猜你喜欢
    • 2015-11-07
    • 2018-03-24
    • 2015-08-18
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2014-04-26
    相关资源
    最近更新 更多