JP7*_*711 3 unit-testing gradle spock build.gradle junit5
我们不能在一个gardle 项目中一起运行使用Junit 5和Spock 框架编写的测试用例?
我们尝试将https://www.baeldung.com/junit-5-gradle 中给出的依赖项添加到我们的 gradle 文件中。Gradle 版本是 4.10.3 和 Junit 5。下面是我的 build.gradle 文件
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven {
url "http://repo.fusesource.com/nexus/content/groups/public/"
}
maven {
url "https://repository.jboss.org/nexus/content/groups/public"
}
jcenter()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile(
'org.codehaus.groovy:groovy-all:2.4.8',
'org.spockframework:spock-core:1.0-groovy-2.4',
'org.jmockit:jmockit:1.8',
'junit:junit:4.12'
)
testRuntime(
'cglib:cglib:2.2.2',
'com.athaydes:spock-reports:1.2.7'
)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly 'junit:junit:4.12'
}
test {
useJUnitPlatform()
testLogging { showStandardStreams = true }
}
Run Code Online (Sandbox Code Playgroud)
我创建了两个测试用例,一个使用 spock 框架,另一个使用 junit 5。但是当我执行 gradlew -test 时,它只运行用 Junit 5 编写的测试用例。下面是构建路径。
您需要 Vintage 测试引擎来执行 Spock 测试,因为它们基于 JUnit 4,并且您需要 Jupiter 测试引擎来执行 JUnit Jupiter 测试。
所以你需要依赖两个引擎。
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
Run Code Online (Sandbox Code Playgroud)
我还建议您升级到 JUnit 5.5.1(即最新最好的)。