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

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()
當然,您還需要在這里配置指令。

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。
添加回答
舉報