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

Prerequisite

Dependencies

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

<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>
<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Note

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.

LoginController

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;

@RestController
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private LoginService service;
	
	
	@PostMapping
        @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;

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

LoginService

package com.codersathi.service;

import org.springframework.stereotype.Service;

import com.codersathi.bean.LoginRequest;

@Service
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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
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;

@WebMvcTest(LoginController.class)
class LoginControllerTest {

	@Autowired
	private MockMvc mockMvc;

	@MockBean
	private LoginService loginService;

	@Test	
	void test_login() throws Exception {
		LoginRequest request = new LoginRequest();
		request.setUsername("myusername");
		request.setPassword("mypassword");

		when(loginService.login(request)).thenReturn("Success");

		mockMvc.perform(post("/login").contentType(MediaType.APPLICATION_JSON)
				.content(new ObjectMapper().writeValueAsString(request))).andExpect(status().isOk()).andReturn();

	}

}

Now, when we execute then our test should pass.

Conclusion

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

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments