使用 JUnit 5 进行弹簧启动器测试

2022-09-01 07:36:22

从 2.0.6 开始使用会带来 JUnit 4 依赖项。我如何使用(通过Gradle),但使用JUnit 5,而不拉入JUnit 4依赖项?spring-boot-starter-testspring-boot-starter-test

如果有帮助,以下是 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 依赖项并执行注释中提到的排除操作成功了。测试依赖项现在如下所示:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'junit', module: 'junit' //by both name and group
}

答案 1

从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 使用不同的包名称,因此它们可以在同一项目中共存。许多注释是相同的(等),因此请确保从包中包含它们。@Testorg.junit.jupiter.api


答案 2

对其他贡献者提到的一些补充说明:

使用 Spring Boot > 2.4.0

如果您使用的是 Spring Boot > ,那么使用 JUnit 5 Jupiter 就无需执行任何操作,因为该库不再包含老式引擎依赖项(可传递地包含 JUnit 4),只需将起始依赖项包含在项目中即可。在 Gradle 配置中使用 。2.4.0spring-boot-starter-testuseJUnitPlatform()

使用 2.4.0 > Spring Boot > 2.2.0

如果你使用早期的版本,我建议使用高于 的版本,这是Spring团队默认添加对JUnit 5 Jupiter的支持的地方。2.2.0.RELEASEspring-boot-starter-test

在这些版本中,该库还包括Vintage Engine依赖项,可用于使用JUnit 5 Jupiter平台运行JUnit 4测试。如果你不需要执行JUnit 4测试,那么spring团队建议排除(不仅仅是描述中指示的):org.junit.vintage:junit-vintage-enginejunit

testCompile('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage'
}

当然,在这里,您还需要配置指令。useJUnitPlatform()


推荐