Spring Boot 2, Mockito and JUnit 5 Example

Learn to write unit tests for the service layer of Spring boot applications using JUnit 5 and Mockito testing frameworks. This tutorial demonstrates spring boot testing service layer example.

1. Maven Dependencies

The spring-boot-starter-test dependency includes JUnit 5 dependencies, and Mockito. So we only need to include this dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
Spring Boot Test Maven Dependency Hierarchy

2. Setup

In this example, we are unit testing primarily two classes EmployeeManager and EmployeeDao. As the name implies, the manager class represents the service layer, and dao class interacts with the database.

EmployeeManager class has a dependency on EmployeeDao and delegate method calls to get the data which is finally returned to controller classes.

To test the methods in EmployeeManager, we can create a JUnit test class TestEmployeeManager in below given two ways:

2.1. MockitoJUnitRunner

MockitoJUnitRunner class automatically initializes all the objects annotated with @Mock and @InjectMocks annotations.

@RunWith(MockitoJUnitRunner.class)
public class TestEmployeeManager {

	@InjectMocks
	EmployeeManager manager;

	@Mock
	EmployeeDao dao;

	//tests
}

2.2. MockitoAnnotations.openMocks() method

If we are not using the MockitoJUnitRunner class approach, then we can use the static method MockitoAnnotations.initMocks(). This method also, upon initialization of junit tests, initializes the mock objects.

public class TestEmployeeManager {

	@InjectMocks
	EmployeeManager manager;

	@Mock
	EmployeeDao dao;

	@Before
	public void init() {
		MockitoAnnotations.openMocks(this);
	}

	//tests
}

2.3. @Mock vs @InjectMocks

  • The @Mock annotation creates a mock implementation for the class it is annotated with.
  • @InjectMocks also creates the mock implementation, additionally injects the dependent mocks that are marked with the annotations @Mock into it.

In the above example, we have annotated EmployeeManager class with @InjectMocks, so mockito will create the mock object for EmployeeManager class and inject the mock dependency of EmployeeDao into it.

3. JUnit Tests using Mockito

Let’s see a few examples of writing the junit tests to unit test the service layer methods using mock objects created with mockito.

A few example methods could be for getAllEmployees() returning a list of EmployeeVO objects, getEmployeeById(int id) for returning an employee by given id; and createEmployee() for adding an employee object and return void.

3.1. Service Layer Tests

package com.howtodoinjava.demo;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import com.howtodoinjava.demo.dao.EmployeeDao;
import com.howtodoinjava.demo.model.EmployeeVO;
import com.howtodoinjava.demo.service.EmployeeManager;

public class TestEmployeeManager {

	@InjectMocks
	EmployeeManager manager;

	@Mock
	EmployeeDao dao;

	@Before
	public void init() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void getAllEmployeesTest()
	{
		List<EmployeeVO> list = new ArrayList<EmployeeVO>();
		EmployeeVO empOne = new EmployeeVO(1, "John", "John", "howtodoinjava@gmail.com");
		EmployeeVO empTwo = new EmployeeVO(2, "Alex", "kolenchiski", "alexk@yahoo.com");
		EmployeeVO empThree = new EmployeeVO(3, "Steve", "Waugh", "swaugh@gmail.com");

		list.add(empOne);
		list.add(empTwo);
		list.add(empThree);

		when(dao.getEmployeeList()).thenReturn(list);

		//test
		List<EmployeeVO> empList = manager.getEmployeeList();

		assertEquals(3, empList.size());
		verify(dao, times(1)).getEmployeeList();
	}

	@Test
	public void getEmployeeByIdTest()
	{
		when(dao.getEmployeeById(1)).thenReturn(new EmployeeVO(1,"Lokesh","Gupta","user@email.com"));

		EmployeeVO emp = manager.getEmployeeById(1);

		assertEquals("Lokesh", emp.getFirstName());
		assertEquals("Gupta", emp.getLastName());
		assertEquals("user@email.com", emp.getEmail());
	}

	@Test
	public void createEmployeeTest()
	{
		EmployeeVO emp = new EmployeeVO(1,"Lokesh","Gupta","user@email.com");

		manager.addEmployee(emp);

		verify(dao, times(1)).addEmployee(emp);
	}
}

The unit test results will be like this.

Spring boot mockito junit example
Spring boot mockito junit example

3.2. Service Layer Class

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.howtodoinjava.demo.dao.EmployeeDao;
import com.howtodoinjava.demo.model.EmployeeVO;

@Service
public class EmployeeManager
{
	@Autowired
	EmployeeDao dao;

	public List&lt;EmployeeVO&gt; getEmployeeList() {
		return dao.getEmployeeList();
	}

	public EmployeeVO getEmployeeById(int id) {
		return dao.getEmployeeById(id);
	}

	public void addEmployee(EmployeeVO employee) {
		dao.addEmployee(employee);
	}
}

3.3. Dao Layer Class

package com.howtodoinjava.demo.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.howtodoinjava.demo.model.EmployeeVO;

@Repository
public class EmployeeDao {

	private Map<Integer, EmployeeVO> DB = new HashMap<>();

	public List<EmployeeVO> getEmployeeList()
	{
		List<EmployeeVO> list = new ArrayList<>();
		if(list.isEmpty()) {
			list.addAll(DB.values());
		}
		return list;
	}

	public EmployeeVO getEmployeeById(int id) {
		return DB.get(id);
	}

	public void addEmployee(EmployeeVO employee) {
		employee.setEmployeeId(DB.keySet().size() + 1);
		DB.put(employee.getEmployeeId(), employee);
	}

	public void updateEmployee(EmployeeVO employee) {
		DB.put(employee.getEmployeeId(), employee);
	}

	public void deleteEmployee(int id) {
		DB.remove(id);
	}
}

4. Conclusion

This mockito tutorial taught us to unit test the service layer in spring boot applications using JUnit and Mockito. We learned to setup the test class and to write JUnit tests.

We also learned the difference between @Mock and @InjectMocks annotations.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

5 thoughts on “Spring Boot 2, Mockito and JUnit 5 Example”

  1. Thanks, great post.

    Hi, you have an error in your post, in the last part you said: “We also learned the difference between @Mock and *@InitMocks * annotations.”

    You should change @InitMocks with @InjectMocks.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.