Use Junit 5 in Spring Boot version 2.1.x

  • |
  • 06 October 2023

Some of the older versions of Spring boot supports Junit 4 (with Junit 5 dependencies as well). If you want to use Junit 5 in in your spring boot application with version 2.1.x, then you can apply the following steps:

In this example I am assuming that you are using spring boot version 2.1.6.RELEASE:

  • First exclude junit dependency in spring-boot-starter-test:
<?xml version="1.0" encoding="UTF-8"?>
<project
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/>
		<!-- lookup parent from repository -->
	</parent>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.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>
	</dependencies>
</project>
  • Then find the appropriate junit 5 version for the spring boot version 2.1.6.RELEASE. You can find the related version by inspecting your pom.xml file. For the version 2.1.6.RELEASE, you have to use junit 5 version 5.3.2. (Junit-5 will not work with other versions):
<?xml version="1.0" encoding="UTF-8"?>
<project
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/>
		<!-- lookup parent from repository -->
	</parent>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<junit-jupiter.version>5.3.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>
	</dependencies>
</project>
  • Add junit-jupiter-engine into your pom.xml (Optional: If you are going to use params, such as @ParametrizedTest, in your test cases, you should also include junit-jupiter-params):
<?xml version="1.0" encoding="UTF-8"?>
<project
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/>
		<!-- lookup parent from repository -->
	</parent>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<junit-jupiter.version>5.3.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-engine</artifactId>
			<version>${junit-jupiter.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-params</artifactId>
			<version>${junit-jupiter.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

That’s it appliying these simple steps, right now junit 5 will be enabled in your project

You May Also Like