Java 使用 JUnit 5 进行 spring-boot-starter-test
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52862349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring-boot-starter-test with JUnit 5
提问by user605331
Using spring-boot-starter-test
as of 2.0.6 brings in a JUnit 4 dependency. How can I use spring-boot-starter-test
(via Gradle), but use JUnit 5 instead, without the JUnit 4 dependency being pulled in?
spring-boot-starter-test
从 2.0.6 开始使用会引入 JUnit 4 依赖项。如何使用spring-boot-starter-test
(通过 Gradle),但使用 JUnit 5,而不引入 JUnit 4 依赖项?
Here's a part of the dependency output from Gradle if it helps:
如果有帮助,以下是 Gradle 依赖项输出的一部分:
+--- org.springframework.boot:spring-boot-starter-test -> 2.0.5.RELEASE
| +--- org.springframework.boot:spring-boot-starter:2.0.5.RELEASE (*)
| +--- org.springframework.boot:spring-boot-test:2.0.5.RELEASE
| | \--- org.springframework.boot:spring-boot:2.0.5.RELEASE (*)
| +--- org.springframework.boot:spring-boot-test-autoconfigure:2.0.5.RELEASE
| | +--- org.springframework.boot:spring-boot-test:2.0.5.RELEASE (*)
| | \--- org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE (*)
| +--- com.jayway.jsonpath:json-path:2.4.0
| | +--- net.minidev:json-smart:2.3
| | | \--- net.minidev:accessors-smart:1.2
| | | \--- org.ow2.asm:asm:5.0.4
| | \--- org.slf4j:slf4j-api:1.7.25
| +--- junit:junit:4.12
| | \--- org.hamcrest:hamcrest-core:1.3
Here is my build.gradlefile:
这是我的build.gradle文件:
buildscript {
ext {
springBootVersion = '2.0.6.RELEASE'
rootGradleDir = "${rootProject.rootDir}/gradle"
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply from: "${rootGradleDir}/staticCodeAnalysis.gradle"
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-jdbc')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-thymeleaf')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.liquibase:liquibase-core')
runtimeOnly('org.springframework.boot:spring-boot-devtools')
runtimeOnly('org.postgresql:postgresql')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.security:spring-security-test')
implementation('org.glassfish.jaxb:jaxb-runtime:2.3.1')
implementation('org.glassfish.jaxb:jaxb-runtime2.3.1')
implementation('org.springframework.boot:spring-boot-starter-data-redis')
testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
testCompile('org.junit.jupiter:junit-jupiter-params:5.3.1')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
UPDATE
更新
Adding the JUnit 5 dependency and doing the exclude mentioned in the comments did the trick. The test dependency now looks like this:
添加 JUnit 5 依赖项并执行注释中提到的排除操作即可解决问题。测试依赖现在看起来像这样:
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit' //by both name and group
}
采纳答案by Mike
As of Gradle 4.6 (I believe), there is native JUnit 5 support. You can just include JUnit5 as follows:
从 Gradle 4.6(我相信)开始,有原生的 JUnit 5 支持。您可以只包含 JUnit5,如下所示:
dependencies {
testCompile "org.junit.jupiter:junit-jupiter-api:5.2.0"
testCompile "org.junit.jupiter:junit-jupiter-params:5.2.0"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"
}
You will also need:
您还需要:
test {
useJUnitPlatform()
}
JUnit 4 and 5 use different package names, so they can co-exist in the same project. Many of the annotations are the same (@Test
, etc) so make sure you include them from the org.junit.jupiter.api
package.
JUnit 4 和 5 使用不同的包名,因此它们可以共存于同一个项目中。许多注释是相同的(@Test
等),因此请确保将它们包含在org.junit.jupiter.api
包中。
回答by Bienvenido David
Here's using implementationinstead of compile.
这是使用implementation而不是compile。
test {
useJUnitPlatform()
}
dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.4.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}
回答by leeyuiwah
Ten months later, and using Gradle 5.4.1
, Spring Boot 2.1.7.RELEASE
, and JUnit 5.5.1
, I found that I was using a different dependency than the other answers. Also, I found that it is better to include the aggregate artifact rather than the individual artifacts:
十个月后,使用Gradle 5.4.1
,Spring Boot 2.1.7.RELEASE
和JUnit 5.5.1
,我发现我使用的依赖项与其他答案不同。此外,我发现最好包含聚合工件而不是单个工件:
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
You can see more about my observation from this other SO Q&A of mine: Interaction between Spring Boot and JUnit 5 — must use the overall artifacts not the individuals?
您可以从我的另一个 SO Q&A 中了解更多关于我的观察:Spring Boot 和 JUnit 5 之间的交互——必须使用整体工件而不是个体吗?
回答by Patrick Herrera
It appears newer versions of spring-boot-starter-test
(noticed in 2.2.6/2.2.7) are importing org.junit.vintage:junit-vintage-engine
, which has a transitive dependency on junit:junit
.
Excluding just junit
gave me some spurious errors:
似乎较新版本的spring-boot-starter-test
(在 2.2.6/2.2.7 中注意到)正在 importing org.junit.vintage:junit-vintage-engine
,它对junit:junit
. 排除只是junit
给了我一些虚假的错误:
May 13, 2020 9:20:05 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: junit/runner/Version
...
Caused by: java.lang.ClassNotFoundException: junit.runner.Version
...
Setting:
环境:
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}
did the trick for me and all tests continue to run fine
对我有用,所有测试都继续正常运行
回答by José Braz
Just add
只需添加
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
And:
和:
test {
useJUnitPlatform()
}