use*_*331 12 java junit spring spring-boot
spring-boot-starter-test从2.0.6开始使用会带来JUnit 4依赖关系.我如何使用spring-boot-starter-test(通过Gradle),但是使用JUnit 5而没有引入JUnit 4依赖?
这是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
Run Code Online (Sandbox Code Playgroud)
这是我的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')
}
Run Code Online (Sandbox Code Playgroud)
添加JUnit 5依赖项并执行注释中提到的排除就可以了.测试依赖项现在看起来像这样:
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit' //by both name and group
}
Run Code Online (Sandbox Code Playgroud)
Ger*_*oza 12
对其他贡献者提到的一些补充说明:
如果您使用的是 Spring Boot > 2.4.0,那么您无需执行任何操作即可使用 JUnit 5 Jupiter,因为该spring-boot-starter-test库不再包含 Vintage-engine 依赖项(它可传递地包含 JUnit 4),只需将 starter 依赖项包含到项目中即可你可以走了。使用useJUnitPlatform()的摇篮配置。
如果您使用早期版本,我建议您使用高于 的版本2.2.0.RELEASE,这是 Spring 团队在spring-boot-starter-test默认情况下添加对 JUnit 5 Jupiter 的支持的地方。
在这些版本中,该库还包含 Vintage Engine 依赖项,可用于使用 JUnit 5 Jupiter 平台运行 JUnit 4 测试。如果您不需要执行 JUnit 4 测试,那么 spring 团队建议排除org.junit.vintage:junit-vintage-engine(不仅仅是junit如描述中所示):
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}
Run Code Online (Sandbox Code Playgroud)
useJUnitPlatform()当然,您还需要在这里配置指令。
Mik*_*ike 10
从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"
}
Run Code Online (Sandbox Code Playgroud)
您还需要:
test {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
JUnit 4和5使用不同的包名称,因此它们可以共存于同一项目中。许多注释是相同的(@Test,等),因此请确保从org.junit.jupiter.api包装中包括它们。
这是使用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'
}
Run Code Online (Sandbox Code Playgroud)
更新 2020-10-29
在我们的 Spring Boot 2.3 项目中,我们不再需要这个片段。默认情况下,它已经在使用 JUnit 5。
似乎较新版本的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
...
Run Code Online (Sandbox Code Playgroud)
环境:
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}
Run Code Online (Sandbox Code Playgroud)
对我来说成功了,所有测试都继续运行良好
| 归档时间: |
|
| 查看次数: |
10951 次 |
| 最近记录: |