Home Junit 5 Spring Boot Junit 5 test example

Spring Boot Junit 5 test example

In this article, we will walk through Spring Boot Junit 5 example application to understand how to do Spring Boot 2 integration test with JUnit 5. Also test a simple GET rest api call using RestTemplate and also test a simple MessageService Spring component.

1. Junit 5

JUnit is one of the most popular unit-testing frameworks to test the Java applications. The JUnit 5 version offers, with the goal to support new features in Java 8 and above, as well as enabling many other testing features.

2. Spring boot Junit 5 example application

2.1. Technologies used :

  1. Junit 5.5.2
  2. Spring Boot 2.2.2.RELEASE
  3. Spring 5.2.2.RELEASE
  4. Maven 3
  5. Java 8
  6. Spring Tool Suite 3.9.8

2.2. Project Structure :

Spring boot junit 5 test application

3. Maven dependencies

<?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">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot2 Junit 5 example</description>

		<!-- Dependency versions -->


		<!-- Jupiter API for writing tests -->

			<!-- Maven plugin to use perticular java version to compile code -->

4. Spring boot components

Following are the Spring components to test with Junit 5.

4.1. MessageService.java

public class MessageService {
	public String getSubscriptionMessage(String user) {
		return "Hello "+user+", Thanks for the subscription!";

4.2. HelloController.java

public class HelloController {
	private MessageService messageService;
	public String sayHello(@RequestParam String user) {
		return messageService.getSubscriptionMessage(user);


4. Test Spring components

4.1. Junit 5 Test Case for MessegeService.java

public class Springboot_Junit5_service_test {

	private MessageService messageService;
	 @DisplayName("Subscription message service test ")
	 void testSubscriptionMessage() {
		String user = "Peter";
	    String message = messageService.getSubscriptionMessage(user);
	    assertEquals("Hello "+user+", Thanks for the subscription!", message);

4.2. Junit 5 Test Case for HelloController.java

Following is the simple GET request API test with RestTemplate.

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class Springboot_junit5_api_test {
	private TestRestTemplate restTemplate;

	 @DisplayName("/hello rest api test ")
	 void testMessage() {
		 String user = "Peter";
		 URI targetUrl= UriComponentsBuilder.fromUriString("/hello")                             
				    .queryParam("user", user)                               
	  String message = this.restTemplate.getForObject(targetUrl, String.class);
	  assertEquals("Hello "+user+", Thanks for the subscription!", message);


You can checkout source code at github.

For complete list of our JUnit 5 tutorials you can checkout JUnit 5 Tutorial.

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook


Please enter your comment!
Please enter your name here

Stay in Touch