Unit Test Spring Boot DELETE REST API using JUnit and Mockito

In this tutorial, we will learn how to write a Unit test for Spring Boot DELETE REST API using JUnit and Mockito framework.

Learn Spring Boot Unit Testing CRUD REST API with JUnit and Mockito.

Spring boot provides spring-boot-starter-test dependency for unit testing and integration testing of Spring boot application:


The Spring Boot Starter Test dependency is a primary dependency for testing the Spring Boot Applications. It holds all the necessary elements required for the testing.

For the Unit testing controller layer REST APIs, we gonna use the following testing libraries:

  • JUnit 5 Framework 
  • Mockito 4 (Latest) 
  • Hamcrest framework 
  • AssertJ Library 
  • JsonPath Library

JUnit 5 Framework

It's the de facto standard testing framework for Java.

The current version of JUnit is 5+. The main goal of JUnit 5 is to support Java 8 and above, as well as enable many different styles of testing.

Mockito 4 (Latest) 

Mockito is a mocking framework. It is a Java-based library used to create simple and basic test APIs for performing unit testing of Java applications.

The main purpose of using the Mockito framework is to simplify the development of a test by mocking external dependencies and using them in the test code.

Learn about the Mockito framework at https://site.mockito.org/

Create Spring Boot Application

Using spring initialize, create a Spring Boot project and add the following dependencies:
  • Spring Web
  • Spring Data JPA
  • Lombok
Generate the Spring boot project as a zip file, extract it, and import it into IntelliJ IDEA.

Make sure that you have added the below dependencies in your spring boot project:

Create JPA Entity

Next, let's create an Employee JPA entity with the following content:
import lombok.*;

import javax.persistence.*;


@Table(name = "employees")
public class Employee {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name", nullable = false)
    private String lastName;

    @Column(nullable = false)
    private String email;
Note that we are using Lombok annotations to reduce the boilerplate code.

Create Repository Layer

Let's create EmployeeRepository which extends the JpaRepository interface:
import net.javaguides.springboot.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {


Create Service Layer


Let's create an EmployeeService interface with CRUD methods:
import net.javaguides.springboot.model.Employee;

import java.util.List;
import java.util.Optional;

public interface EmployeeService {
    void deleteEmployee(long id);


Let's create an EmployeeServiceImpl class that implements the EmployeeService interface:

import net.javaguides.springboot.exception.ResourceNotFoundException;
import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.repository.EmployeeRepository;
import net.javaguides.springboot.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

public class EmployeeServiceImpl implements EmployeeService {

    private EmployeeRepository employeeRepository;

    public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;

    public void deleteEmployee(long id) {

Controller Layer - DELETE REST API

import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class EmployeeController {

    private EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;

    public ResponseEntity<String> deleteEmployee(@PathVariable("id") long employeeId){


        return new ResponseEntity<String>("Employee deleted successfully!.", HttpStatus.OK);


Writing Unit Test Spring Boot DELETE REST API

Now, let's create a Unit test for Spring boot DELETE REST API. We gonna use the @WebMvcTest annotation to load only EmployeeController class.

import com.fasterxml.jackson.databind.ObjectMapper;
import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.service.EmployeeService;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

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

import static org.hamcrest.CoreMatchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

public class EmployeeControllerTests {

    private MockMvc mockMvc;

    private EmployeeService employeeService;

    private ObjectMapper objectMapper;

// JUnit test for delete employee REST API
    public void givenEmployeeId_whenDeleteEmployee_thenReturn200() throws Exception{
        // given - precondition or setup
        long employeeId = 1L;

        // when -  action or the behaviour that we are going test
        ResultActions response = mockMvc.perform(delete("/api/employees/{id}", employeeId));

        // then - verify the output
We are using @MockBean annotation to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context.

The @MockBean annotation tells Spring to create a mock instance of EmployeeService and add it to the application context so that it's injected into EmployeeController. We have a handle on it in the test so that we can define its behavior before running each test.

Note that we are using MockMvc class to make REST API calls.

We are using ResultActions class to handle the response of the REST API.

We are using Mockito to stub the method calls.


Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course