git change remote url

How to write Unit Test for Controller layer in Spring Boot?

In this post, we will learn to write a Unit Test for the Controller layer of our spring boot application. We will be using the following version of our Spring Boot and Java.

  • Spring Boot: 2.6.2
  • JDK 11



In this example, we need the following dependencies. Make sure that these dependencies are available in your pom.xml file.



Most of the time, the spring-boot-starter-test dependency will be added automatically when you add spring-boot-starter-web.
  • The spring-boot-starter-web dependency is used for building the web application, including RESTful applications using Spring MVC. It uses Tomcat as the default embedded container.
  • The spring-boot-starter-test dependency contains all the required dependencies to run our test. This is the magic of using Spring Boot :).
  • The lombok dependency is a Java annotation library which helps us to reduce boilerplate code in our classes.
  • The spring-boot-starter-validation dependency is uses to validate oru Beans.


package com.codersathi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codersathi.bean.LoginRequest;
import com.codersathi.service.LoginService;

public class LoginController {

	private LoginService service;
        @ResponseStatus(code = HttpStatus.OK)
	public String login(@Validated @RequestBody LoginRequest request) {
		return service.login(request);

LoginRequest bean

package com.codersathi.bean;

import javax.validation.constraints.NotBlank;

import lombok.Data;

public class LoginRequest {
	@NotBlank(message = "username is required")
	private String username;
	@NotBlank(message = "password is required")
	private String password;


package com.codersathi.service;

import org.springframework.stereotype.Service;

import com.codersathi.bean.LoginRequest;

public class LoginService {
	public String login(LoginRequest loginRequest) {
		return "We don't care it while testing our controller";

Now, we have created our login endpoint, and when the user sends the username and password in POST request it will return the Success message as a response.

In our example, we have also added a bean validation so that, if the user tries to send a request without providing the username and password in the request body it will throw an exception saying these values are required.

So, let’s test our controller to make sure that our API endpoint validates the request and sends the success message regardless of the response from LoginService.

Since we are trying to write a Unit test for our LoginController we will be mocking the LoginService and expect a successful response.

Create a Test case for our LoginController

package com.codersathi;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import static;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

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.MvcResult;

import com.codersathi.bean.LoginRequest;
import com.codersathi.controller.LoginController;
import com.codersathi.service.LoginService;
import com.fasterxml.jackson.databind.ObjectMapper;

class LoginControllerTest {

	private MockMvc mockMvc;

	private LoginService loginService;

	void test_login() throws Exception {
		LoginRequest request = new LoginRequest();


				.content(new ObjectMapper().writeValueAsString(request))).andExpect(status().isOk()).andReturn();



Now, when we execute then our test should pass.


In this post, we learned to write unit test the Controller layer of the Spring Boot application

Notify of
Inline Feedbacks
View all comments