2014-03-31 107 views
6

我正在做Android項目(在Android Studio中),裏面有一個小的SDK,那就是純java。Android Studio錯誤JUNIT4 !!!預計JUnit版本3.8或更高版本:

所以我想要做的是從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測試特定的行爲,這意味着它不工作

回答

3

如果您需要在設備上運行測試(硬件或模擬器),則不能使用JUnit 4,因爲它不是該版本的內置組件。你需要使用JUnit 3(我知道這很麻煩)。 如果您有一些不需要運行Android的測試,則可以使用您喜歡的任何版本(如使用標準JUnit測試)。

+0

我不需要測試Android代碼(目前)。只需要測試連接到服務器並獲得JSON的純Java代碼。在android studio中,我不運行所有的項目,只是要測試這個測試類,然後發生錯誤。 – Shudy

+2

Android插件在非Android測試中表現不佳。最好的辦法是將純Java代碼分離成一個純粹的Java模塊,該模塊由Gradle中的'java'插件構建。然後,無論你喜歡,你都可以測試它。 –

相關問題