Junit 5 and Spring boot 2.1.X-Maven importing wrong JUnit version

If you are trying to use JUnit 5.5.2 together with Spring Boot 2.1.X, and find out that you are missing some JUnit classes (because you are actually not using 5.5.2), we might have a solution to your problem.

A couple of days ago, I was writing a unit test and surprisingly the IDE complained it couldn’t find where to import from interface for @NullSource. Well, I knew it does exists in JUnit 5, I’ve used it before and it is on JUnit Javadoc for the version 5.5.2.
This is one of those moments when we scratch our head with a “What the heck”

I revised the Pom file, just to make sure I had not made any mistake in the version, and it was correct, 5.5.2 (at the time of this post, this is the latest stable version). I had defined it in the Pom properties as <version.junit>5.5.2</version.junit> and was also excluding JUnit from the spring-boot-starter-test. The dependencies section also had the corresponding

1
2
3
4
5
6
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>

so, everything looked correct.

A quick look into the .m2 folder, revealed the presence of version 5.3.2, so I deleted it and run maven clean package on the project while monitoring the .m2/repository/org/junit/jupiter/junit-jupiter-params folder. To my surprise, version 5.3.2 magically reappeared. Unfortunately, this is one of those things we now will make us lose some precious time while trying to pinpoint the problem.

In another project I had JUnit working perfectly with version 5.5.2, but the “offending” project was using Spring boot 2.1.7.RELEASE and the other wasn’t, so I focused my attention on that.

The solution

By checking Spring boot’s 2.1.7-RELEASE dependencies, we can verify it imports <junit-jupiter.version>5.3.2</junit-jupiter.version>, and that’s not the version we want.

The solution is then, very simple.

All we have to do in our project’s Pom file, is to change the property name to <junit-jupiter.version> to match the same name as the Spring boot’s one, naming the dependency version variable accordingly.
Maven should now import the correct version to our project.

If you come across this problem make sure your pom includes the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
...
<properties>
...
<junit-jupiter.version>5.5.2</junit-jupiter.version>
...
</properties>
...

<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
...
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
...
</dependencies>

We will find this behavior with Spring boot versions lower than 2.1.7, in 2.1.8 and 2.1.9.

With the release of Spring-boot 2.2.0 the problem will not occur, as it already imports JUnit 5.5.2.

Hope this helps.

happy coding.

Share