#15 Testing in Spring Boot

Testing is an essential aspect of software development that ensures the reliability and stability of your applications. Spring Boot provides excellent support for testing, making it easier to write unit tests, integration tests, and tests for REST APIs. In this article, we will explore how to perform unit testing with JUnit and Mockito, integration testing, and testing REST APIs with Spring Boot.

Unit Testing with JUnit and Mockito

Unit testing focuses on testing individual components of the application in isolation. JUnit is a popular testing framework for Java, and Mockito is a mocking framework that allows you to create mock objects for testing purposes.

JUnit Setup: To start with JUnit, add the following dependencies to your pom.xml or build.gradle file:




testImplementation 'org.springframework.boot:spring-boot-starter-test'

Example Unit Test with JUnit and Mockito:

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;

public class UserServiceTest {

    private UserRepository userRepository;

    private UserService userService;

    public void testGetUserById() {
        User user = new User(1L, "John", "john@example.com");

        User found = userService.getUserById(1L);
        assertEquals("John", found.getName());

In this example, MockitoAnnotations.initMocks(this) initializes the mocks, and @InjectMocks injects the mock UserRepository into the UserService.

Integration Testing

Integration testing verifies the interaction between different components of the application. Spring Boot makes it easy to write integration tests using the @SpringBootTest annotation, which loads the complete application context for testing.

Example Integration Test:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerIntegrationTest {

    private TestRestTemplate restTemplate;

    public void testGetUser() {
        ResponseEntity<User> response = restTemplate.getForEntity("/api/users/1", User.class);
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertEquals("John", response.getBody().getName());

In this example, @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) starts the application on a random port, and TestRestTemplate is used to make HTTP requests to the application.

Testing REST APIs with Spring Boot

Spring Boot provides excellent support for testing REST APIs. You can use MockMvc to perform requests and verify responses without starting the server.

Example REST API Test:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

public class UserControllerTest {

    private MockMvc mockMvc;

    public void setup() {
        // Setup mock data if needed

    public void testGetUser() throws Exception {

    public void testCreateUser() throws Exception {
        String userJson = "{\"name\":\"John\",\"email\":\"john@example.com\"}";

In this example, @AutoConfigureMockMvc is used to configure MockMvc, and we perform GET and POST requests to the /api/users endpoint, verifying the responses.


Testing in Spring Boot is streamlined and efficient, thanks to its robust support for unit testing, integration testing, and testing REST APIs. Using JUnit and Mockito for unit tests, @SpringBootTest for integration tests, and MockMvc for REST API tests, you can ensure your application is reliable and performs as expected. By incorporating comprehensive testing practices, you can significantly improve the quality and stability of your Spring Boot applications.


#SpringBoot #Java #Testing #JUnit #Mockito #IntegrationTesting #RESTAPI #MockMvc #SoftwareTesting #UnitTesting #JavaDevelopment #SpringFramework #Programming #Coding #LearnJava #Tech

Leave a Reply