HomeJunit 5Spring 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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javabydeveloper</groupId>
	<artifactId>Spring-Boot-Junit5-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring-Boot-Junit5-example</name>
	<description>Spring boot2 Junit 5 example</description>

	<properties>
		<!-- Dependency versions -->
		<junit.jupiter.version>5.5.2</junit.jupiter.version>
		<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
		
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<!-- Jupiter API for writing tests -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- Maven plugin to use perticular java version to compile code -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4. Spring boot components

Following are the Spring components to test with Junit 5.

4.1. MessageService.java

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

4.2. HelloController.java

@RestController
public class HelloController {
	
	@Autowired
	private MessageService messageService;
	
	@GetMapping("/hello")
	public String sayHello(@RequestParam String user) {
		return messageService.getSubscriptionMessage(user);
	}

}

4. Test Spring components

4.1. Junit 5 Test Case for MessegeService.java

@SpringBootTest
public class Springboot_Junit5_service_test {

	@Autowired
	private MessageService messageService;
	
	 @Test
	 @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 {
	
	@Autowired
	private TestRestTemplate restTemplate;

	 @Test
	 @DisplayName("/hello rest api test ")
	 void testMessage() {
		 
		 String user = "Peter";
		 URI targetUrl= UriComponentsBuilder.fromUriString("/hello")                             
				    .queryParam("user", user)                               
				    .build()                                                
				    .encode()                                               
				    .toUri();
		 
	  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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here