Search Tutorials

Spring Boot JUnit Test Simple Example | JavaInUse

Spring Boot Unit Test Simple Example

In this post we will write test cases for a Spring Boot Application. Spring Boot provides a number of utilities and annotations to help test a Spring Boot Application.
Spring Boot Test is provided by two modules -
  • spring-boot-test contains core items
  • spring-boot-test-autoconfigure supports auto-configuration for tests
spring-boot-starter-test dependency imports both the above Spring Boot test modules as well has JUnit, AssertJ, Hamcrest and a number of other useful libraries.


This tutorial is explained in the below Youtube Video.

Lets Begin-

Maven Project will be as follows-

Spring Boot Unit Testing Tutorial

In the Maven we need the spring boot test dependency.Maven will be as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->







Create the as below-
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {, args);
Create the Employee model class as follows-
package com.javainuse.model;

public class Employee {
	private String empId;
	private String name;
	private String designation;
	private double salary;

	public Employee() {

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getDesignation() {
		return designation;

	public void setDesignation(String designation) {
		this.designation = designation;

	public double getSalary() {
		return salary;

	public void setSalary(double salary) {
		this.salary = salary;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;


@RequestMapping maps /employee request to return an employee object.
package com.javainuse.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;

public class TestController {

	@RequestMapping(value = "/employee", method = RequestMethod.GET)
	public Employee firstPage() {

		Employee emp = new Employee();

		return emp;

Compile and the run the as a Java application.
Go to localhost:8080/employee
Spring Boot JUnit Example
Next we write JUnit test cases for the TestController.
Define the as follows. Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.
package com.javainuse.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

public class SpringBootHelloWorldTests {

	public void contextLoads() {


Next extend the and write the test case for the
package com.javainuse.test;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

public class TestWebApp extends SpringBootHelloWorldTests {

	private WebApplicationContext webApplicationContext;

	private MockMvc mockMvc;

	public void setup() {
		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

	public void testEmployee() throws Exception {


Run this TestWebApp class as a JUnit test case.
Spring Boot Unit Testing Example

Download Source Code

Download it -
Spring Boot + Unit Cases

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions