使用JUnit 5进行spring-boot-starter-test
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
这是我的 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')
}
更新
添加JUnit 5依赖项并执行注释中提到的exclude可以解决问题。现在,测试依赖项如下所示:
testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'junit', module: 'junit' //by both name and group
}
回答:
从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"
}
您还需要:
test { useJUnitPlatform()
}
JUnit
4和5使用不同的包名称,因此它们可以共存于同一项目中。许多注释是相同的(@Test
,等),因此请确保从org.junit.jupiter.api
包装中包括它们。
以上是 使用JUnit 5进行spring-boot-starter-test 的全部内容, 来源链接: utcz.com/qa/436160.html