Back to Repositories

Testing Spring Boot REST Controller Implementation in SpringBoot-Labs

This test suite demonstrates comprehensive unit testing of a Spring Boot REST controller using MockMvc and Mockito frameworks. It validates user retrieval endpoints and response handling while implementing proper mocking patterns.

Test Coverage Overview

The test suite provides thorough coverage of the UserController’s GET endpoint functionality.

Key areas tested include:
  • User retrieval by ID
  • Response status code validation
  • JSON response structure verification
  • Individual field value validation
Integration points with UserService are properly mocked to isolate controller testing.

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests and validating responses. Mockito is employed for service layer mocking, demonstrating clean separation of concerns.

Notable patterns include:
  • MockBean annotation for service dependency injection
  • Multiple response validation techniques
  • Structured request building
  • Both complete and granular JSON response verification

Technical Details

Testing tools and configuration:
  • JUnit 4 with SpringRunner
  • @SpringBootTest for application context
  • @AutoConfigureMockMvc for MockMvc setup
  • Mockito for service mocking
  • MockMvc for HTTP request simulation
  • JsonPath for response validation
  • Hamcrest matchers for assertions

Best Practices Demonstrated

The test implementation showcases several testing best practices in Spring Boot applications.

Notable practices include:
  • Proper test isolation through mocking
  • Multiple assertion strategies for thorough validation
  • Clear test method naming
  • Focused test scope
  • Clean setup using Spring Boot test annotations
  • Comprehensive response validation

yudaocode/springboot-labs

lab-42/lab-42-demo01/src/test/java/cn/iocoder/springboot/lab23/testdemo/controller/UserControllerTest.java

            
package cn.iocoder.springboot.lab23.testdemo.controller;

import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import cn.iocoder.springboot.lab23.testdemo.service.UserService;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * UserController 单元测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private UserService userService;

    @Test
    public void testGet() throws Exception {
        // Mock UserService 的 get 方法
        Mockito.when(userService.get(1)).thenReturn(
                new UserDO().setId(1).setUsername("username:1").setPassword("password:1"));

        // 查询用户
        ResultActions resultActions = mvc.perform(MockMvcRequestBuilders.get("/user/get?id=1"));

        // 校验响应状态码
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()); // 响应状态码 200

        // 校验响应内容方式一:直接全部匹配
        resultActions.andExpect(MockMvcResultMatchers.content().json("{\n" +
                "    \"id\": 1,\n" +
                "    \"username\": \"username:1\",\n" +
                "    \"password\": \"password:1\"\n" +
                "}", true)); // 响应结果

        // 校验响应内容方式二:逐个字段匹配
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("id", IsEqual.equalTo(1)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("username", IsEqual.equalTo("username:1")));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("password", IsEqual.equalTo("password:1")));
    }

}