使用JUnit 5进行spring-boot-starter-test [英] spring-boot-starter-test with JUnit 5

查看:1552
本文介绍了使用JUnit 5进行spring-boot-starter-test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从2.0.6版本开始使用spring-boot-starter-test会带来JUnit 4依赖关系.如何使用spring-boot-starter-test(通过Gradle),而使用JUnit 5,而又不引入JUnit 4依赖项?

Using spring-boot-starter-test as of 2.0.6 brings in a JUnit 4 dependency. How can I use spring-boot-starter-test (via Gradle), but use JUnit 5 instead, without the JUnit 4 dependency being pulled in?

如果有帮助,这是Gradle依赖项输出的一部分:

Here's a part of the dependency output from Gradle if it helps:

+--- 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 文件:


Here is my build.gradle file:

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可以解决问题.现在,测试依赖项如下所示:


UPDATE

Adding the JUnit 5 dependency and doing the exclude mentioned in the comments did the trick. The test dependency now looks like this:

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

推荐答案

从Gradle 4.6开始(我相信),已经有本机JUnit 5支持.您可以仅包含JUnit5,如下所示:

As of Gradle 4.6 (I believe), there is native JUnit 5 support. You can just include JUnit5 as follows:

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 4 and 5 use different package names, so they can co-exist in the same project. Many of the annotations are the same (@Test, etc) so make sure you include them from the org.junit.jupiter.api package.

这篇关于使用JUnit 5进行spring-boot-starter-test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆