spring-boot-starter-test with JUnit 5
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"
}
You will also need:
test {
useJUnitPlatform()
}
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.
A few additional notes to the ones mentioned by other contributors:
Using Spring Boot > 2.4.0
If you are using Spring Boot > 2.4.0
, then there is nothing you have to do to use JUnit 5 Jupiter, because the spring-boot-starter-test
library no longer includes the vintage-engine dependency (which transitively included JUnit 4), just include the starter dependency to the project and you're good to go. Use the useJUnitPlatform()
in the Gradle configuration.
Using 2.4.0 > Spring Boot > 2.2.0
If you use earlier versions, I'd suggest using a version higher than 2.2.0.RELEASE
, which is where the Spring team added support for JUnit 5 Jupiter into the spring-boot-starter-test
by default.
In these versions, the library included the Vintage Engine dependency too, which could be used to run JUnit 4 tests using the JUnit 5 Jupiter platform. If you don't need to execute JUnit 4 tests, then the spring team suggests excluding org.junit.vintage:junit-vintage-engine
(not just junit
as indicated in the description):
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage'
}
Here you would also need to configure the useJUnitPlatform()
directive, of course.