Getting Started with JUnit 5: Test your Spring MVC Application with it

In this post, we described how to use JUnit 5 to test your Spring MVC application with Maven.

Setup Maven Dependencies

We can get the required dependencies by declaring the junit-jupiter-api (version 5.x) dependency in our pom.xml file. This dependency provides the public API for writing tests and extensions.

<dependency>
	<groupId>org.junit.jupiter</groupId>
	<artifactId>junit-jupiter-engine</artifactId>
	<version>5.1.0</version>
	<scope>test</scope>
</dependency>

If we are using an IDE that is bundled with an older JUnit 5 version, it throws an exception when we try to run our unit tests by using our IDE. We can fix this problem by adding the following dependency to our POM file:

<dependency>
	<groupId>org.junit.platform</groupId>
	<artifactId>junit-platform-launcher</artifactId>
	<version>1.1.0</version>
	<scope>test</scope>
</dependency>

The junit-platform-launcher provides a public API for configuring and launching tests. The API is typically used by IDEs and build tools.

To test Spring application, we need to add the following dependencies:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>${spring.version}</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>com.github.sbrannen</groupId>
	<artifactId>spring-test-junit5</artifactId>
	<version>1.0.3</version>
	<scope>test</scope>
</dependency>

The spring-test-junit5 doesn't exist in the Maven Central Repository but jitpack.io. So we need to add the following repository to pom.xml:

<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://jitpack.io</url>
	</repository>
</repositories>

After we have declared the required dependencies, we need to configure the Maven Surefire Plugin. Let’s find out how we can do it.

Configure Maven Surefire Plugin

We have to declare the maven-surefire-plugin (version 2.19.1) in our pom.xml file and configure the dependencies of this plugin. Please note that the latest version (2.20.1) doesn't work well with JUnit 5.

We have to declare the following plugin in plugins section in pom.xml:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.19.1</version>
	<dependencies>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-surefire-provider</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.1.0</version>
		</dependency>
	</dependencies>
</plugin>

We have now created a Maven project that can run unit tests which use JUnit 5. Let’s move on and write a simple unit test with JUnit 5.

Write Simple Tests

Before we can write unit tests which use JUnit 5, we have to know these two things:

  • The src/test/java directory contains the source code of our unit tests.
  • The src/test/resources directory contains the resources of our unit tests.

Let’s create a new test class and add one test method to created class. A simple test cases can be written as follows:

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.function.Executable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;

import org.verwandlung.voj.web.model.EmailValidation;

import java.util.Date;

@ExtendWith(SpringExtension.class)
@Transactional
@ContextConfiguration({ "classpath:test-spring-context.xml" })
public class SampleTest {
	@BeforeAll
	public static void setUp() {
		// Do something before all tests
	}

	@AfterAll
	public static void tearDown() {
		// Do something after all tests
	}

	@Test
	public void testAdd() {
		int a = 1, b = 1;
		Assertions.assertEquals(2, a + b);
	}

	@Test
	public void testDatabaseQuery() {
		EmailValidation emailValidation = new EmailValidation("support@verwandlung.org", "RandomToken", new Date());
		Executable e = () -> {
			emailValidationMapper.createEmailValidation(emailValidation);
		};
		Assertions.assertThrows(org.springframework.dao.DataIntegrityViolationException.class, e);
	}

	@Autowired
	private EmailValidationMapper emailValidationMapper;
}

Reference

  • https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-maven/
  • https://stackoverflow.com/questions/41747849/junit-5-and-spring-framework-4-3-x
  • https://stackoverflow.com/questions/40268446/junit-5-how-to-assert-an-exception-is-thrown/42189160
  • https://stackoverflow.com/questions/36970384/surefire-is-not-picking-up-junit-5-tests
  • https://junit.org/junit5/docs/5.1.0/api/org/junit/jupiter/api/BeforeAll.html
Contact Us
  • Room 614, Zonghe Building, Harbin Institute of Technology
  • cshzxie [at] gmail.com