6

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

Shudy
  • 7,806
  • 19
  • 63
  • 98

1 Answers1

3

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).

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • I don't need to test Android code (for the moment). Just need to test, pure java code, that connects to server and get a JSON. in android studio, I don't run All the project, just this test class to be tested, and then the error happens. – Shudy Mar 31 '14 at 09:34
  • 2
    The Android plugin doesn't do well with non-Android testing. Your best bet is probably to separate the pure Java code into a pure Java module that's built with the `java` plugin in Gradle. Then you can test it however you like. – Scott Barta Mar 31 '14 at 17:03