Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot-starter-test with JUnit 5

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?

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 


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') } 

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 } 
like image 367
user605331 Avatar asked Oct 17 '18 19:10

user605331


People also ask

Does spring boot starter test include JUnit 5?

By default, spring-boot-starter-test dependency imports the junit 4 dependencies into Spring boot application. To use Junit 5, we must exclude Junit 4 and include Junit 5 into project dependencies.

Does spring boot starter test include JUnit?

Spring Boot provides a number of useful tools for testing your application. The spring-boot-starter-parent POM provides JUnit, Hamcrest and Mockito “test” scope dependencies.

What is the difference between junit4 and JUnit 5?

Differences Between JUnit 4 and JUnit 5 Some other differences include: The minimum JDK for JUnit 4 was JDK 5, while JUnit 5 requires at least JDK 8. The @Before , @BeforeClass , @After , and @AfterClass annotations are now the more readable as the @BeforeEach , @BeforeAll , @AfterEach , and @AfterAll annotations.


1 Answers

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.

like image 136
Mike Avatar answered Sep 23 '22 04:09

Mike