问题描述
我正在做一个Android项目(在Android Studio中),里面有一个纯Java的小SDK.
I'm doing Android project (in Android Studio), with a little SDK inside that is pure java.
所以我要从JUnit4做测试.
So What I want is to do test from 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')
}
然后,我做一个简单的测试用例,以查看结膜炎是否起作用.但是当我运行它时,会提示此错误.
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:
在其他问题中,我看到了解决方案,将Junit4.jar的依赖关系更改为第一位,但这种方法不起作用
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
推荐答案
如果您测试Android特定的行为,这意味着如果您需要在设备(硬件或仿真器)上运行测试,则无法使用JUnit 4,因为它是不在该版本中内置.您需要使用JUnit 3(我知道这很麻烦).如果您有一些不需要运行Android的测试,则可以使用所需的每个版本(就像使用标准JUnit测试一样).
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).
这篇关于Android Studio错误JUNIT4 !!!预期的JUnit版本3.8或更高版本:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!