Android Studio Error JUNIT4 !!! JUnit version 3.8

2019-06-20 05:33发布

I'm doing Android project (in Android Studio), with a little SDK inside that is pure java.

So What I want is to do test from JUnit4.

And I configured the Gradlle File in this way:

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')
}

Then I do a simply test case to see if junitis working. But when I run it, this error is promted.

!!! JUnit version 3.8 or later expected:

I saw in some other question that the solutions was, change dependences of Junit4.jar to be in first position, but it doesn't work to

1条回答
爷、活的狠高调
2楼-- · 2019-06-20 05:57

If you test Android specific behaviour, that means if you need to run the test on the device (hardware or Emulator), you cannot use JUnit 4, because it's not built in in that version. You need to use JUnit 3 (I know that's cumbersome). If you have some tests where you don't need a running Android, you can use every version you like (as you do with standard JUnit tests).

查看更多
登录 后发表回答