首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >弹簧-启动-启动-使用JUnit 5的测试

弹簧-启动-启动-使用JUnit 5的测试
EN

Stack Overflow用户
提问于 2018-10-17 19:33:29
回答 5查看 46.2K关注 0票数 34

在2.0.6中使用spring-boot-starter-test会带来JUnit 4依赖关系。我如何使用spring-boot-starter-test (通过Gradle),而不是使用JUnit 5,而不使用JUnit 4依赖项呢?

下面是Gradle提供的依赖项输出的一部分,如果有帮助的话:

代码语言:javascript
复制
+--- 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文件:

代码语言:javascript
复制
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依赖项并执行注释中提到的排除操作就可以了。测试依赖项现在看起来如下所示:

代码语言:javascript
复制
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'junit', module: 'junit' //by both name and group
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-10-17 19:44:24

从Gradle 4.6 (我相信)开始,就有了原生的JUnit 5支持。您只需将JUnit5包括如下:

代码语言:javascript
复制
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"
}

你还需要:

代码语言:javascript
复制
test {
  useJUnitPlatform()
}

JUnit 4和5使用不同的包名,因此它们可以在同一个项目中共存。许多注释是相同的(@Test等),所以请确保从org.junit.jupiter.api包中包含它们。

票数 18
EN

Stack Overflow用户

发布于 2021-01-04 14:38:28

关于其他撰稿人提到的问题,还有几点说明:

使用Spring > 2.4.0

如果您使用的是Spring > 2.4.0,那么使用JUnit 5木星没有什么需要做的,因为spring-boot-starter-test库不再包含老式引擎依赖项(其中临时包含了JUnit 4),只需包含项目的初始依赖项,您就可以继续了。在Gradle配置中使用useJUnitPlatform()

使用2.4.0 > Spring > 2.2.0

如果您使用早期版本,我建议使用高于2.2.0.RELEASE的版本,这是Spring默认在spring-boot-starter-test中添加了对JUnit 5木星的支持的地方。

在这些版本中,库还包括了Vintage引擎依赖项,它可以用于使用JUnit 5木星平台运行JUnit 4测试。如果不需要执行JUnit 4测试,那么spring团队建议排除org.junit.vintage:junit-vintage-engine (而不仅仅是描述中所示的junit ):

代码语言:javascript
复制
testCompile('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage'
}

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

票数 19
EN

Stack Overflow用户

发布于 2019-05-21 14:44:49

这里使用的是实现而不是编译。

代码语言:javascript
复制
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'
}

更新2020-10-29

在我们的Spring 2.3项目中,我们不再需要这个片段了。默认情况下,它已经在使用JUnit 5。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52862349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档