Java 使用JUnit 5进行spring启动程序测试
从2.0.6开始使用Java 使用JUnit 5进行spring启动程序测试,java,spring,spring-boot,junit,Java,Spring,Spring Boot,Junit,从2.0.6开始使用springbootstarter测试,带来了JUnit4依赖性。我如何使用springbootstartertest(通过Gradle),而不是使用junit5,而不引入junit4依赖项 下面是Gradle的依赖项输出的一部分,如果有帮助的话: +--- org.springframework.boot:spring-boot-starter-test -> 2.0.5.RELEASE | +--- org.springframework.boot:sprin
springbootstarter测试
,带来了JUnit4依赖性。我如何使用springbootstartertest
(通过Gradle),而不是使用junit5,而不引入junit4依赖项
下面是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
这是我的build.gradle文件:
更新 添加JUnit5依赖项并执行注释中提到的排除操作就成功了。测试依赖项现在如下所示:
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit' //by both name and group
}
从Gradle4.6开始(我相信),就有了原生的JUnit5支持。您可以只包含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"
}
您还需要:
test {
useJUnitPlatform()
}
JUnit4和JUnit5使用不同的包名,因此它们可以共存于同一个项目中。许多注释都是相同的(@Test
,等等),因此请确保从org.junit.jupiter.api
包中包含它们。这里使用的是实现而不是编译
更新2020-10-29
在我们的SpringBoot2.3项目中,我们不再需要这个片段了。默认情况下,它已经在使用JUnit 5了。十个月后,使用
Gradle 5.4.1
、Spring Boot 2.1.7.RELEASE
、以及JUnit 5.5.1
,我发现我使用的依赖项与其他答案不同。此外,我发现最好包含聚合工件,而不是单个工件:
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
你可以从我的另一个问答中看到更多关于我的观察:似乎较新版本的
spring boot starter test
(在2.2.6/2.2.7中注意到)正在导入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
...
设置:
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}
我的技巧和所有测试都继续正常运行了吗
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
以及:
除了其他贡献者提到的注释外,还有一些其他注释: 使用Spring Boot>2.4.0 如果您使用的是Spring Boot>
2.4.0
,那么使用JUnit 5 Jupiter就无需做任何事情,因为Spring Boot starter test
库不再包含老式引擎依赖项(它过渡地包含JUnit 4),只需将starter依赖项包含到项目中即可。在Gradle配置中使用useJUnitPlatform()
使用2.4.0>Spring Boot>2.2.0
如果您使用早期版本,我建议使用高于2.2.0.RELEASE
的版本,在默认情况下,Spring团队将JUnit 5 Jupiter支持添加到Spring启动测试中
在这些版本中,库还包含了Vintage引擎依赖项,可用于使用JUnit 5 Jupiter平台运行JUnit 4测试。如果您不需要执行JUnit4测试,那么spring团队建议排除org.JUnit.vintage:JUnit vintage引擎
(而不仅仅是JUnit
,如描述所示):
当然,您还需要在这里配置useJUnitPlatform()
指令。检查此项。您只需要排除junit4@lealceldeiro只要试一下就行了。谢谢谢谢我也这么做了,但是JUnit4依赖项仍然存在。听起来@lealcelderio提到的排除路线就是这里的方法。正确的,如果你根本不希望它被包括在内,你需要排除它。
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
test {
useJUnitPlatform()
}
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}