亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JUnit 5 進行 spring-boot-starter-test

使用 JUnit 5 進行 spring-boot-starter-test

Cats萌萌 2021-11-24 15:09:10
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.8repositories {    mavenCentral()}test {  useJUnitPlatform()}
查看完整描述

3 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

從 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包中。


查看完整回答
反對 回復 2021-11-24
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

對其他貢獻者提到的一些補充說明:

使用 Spring Boot > 2.4.0

如果您使用的是 Spring Boot > 2.4.0,那么您無需執行任何操作即可使用 JUnit 5 Jupiter,因為該spring-boot-starter-test庫不再包含 Vintage-engine 依賴項(它可傳遞地包含 JUnit 4),只需將 starter 依賴項包含到項目中即可你可以走了。使用useJUnitPlatform()的搖籃配置。

使用 2.4.0 > Spring Boot > 2.2.0

如果您使用早期版本,我建議您使用高于 的版本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'}

useJUnitPlatform()當然,您還需要在這里配置指令。


查看完整回答
反對 回復 2021-11-24
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

這是使用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'

}

更新 2020-10-29


在我們的 Spring Boot 2.3 項目中,我們不再需要這個片段。默認情況下,它已經在使用 JUnit 5。


查看完整回答
反對 回復 2021-11-24
  • 3 回答
  • 0 關注
  • 337 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號