Back to Repositories

Testing Spring Boot User Controller REST Endpoints in spring-boot-examples

This test suite demonstrates integration testing of a User Controller in a Spring Boot application using MockMvc. It validates REST endpoint functionality through simulated HTTP requests and verifies proper JSON response handling.

Test Coverage Overview

The test coverage focuses on validating the User Controller’s REST endpoints, specifically the /getUsers POST endpoint. Key functionality includes:

  • HTTP POST request handling
  • JSON response processing
  • UTF-8 encoding support
  • Request parameter validation

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without deploying a full server. The implementation leverages Spring Boot’s testing annotations (@SpringBootTest) and JUnit’s testing lifecycle management (@Before) to ensure proper test context initialization.

The test pattern demonstrates the builder pattern for MockMvc setup and fluent API usage for request construction.

Technical Details

  • Testing Framework: JUnit 4
  • Spring Test Framework: MockMvc
  • Runner: SpringRunner
  • Key Annotations: @RunWith, @SpringBootTest, @Autowired
  • Content Type: APPLICATION_JSON_UTF8
  • Test Setup: WebApplicationContext configuration

Best Practices Demonstrated

The test class exemplifies several testing best practices in Spring Boot applications. It demonstrates proper test isolation through MockMvc setup, clear test method naming, and appropriate use of Spring’s testing annotations.

  • Proper test context setup using @Before
  • Clean separation of concerns
  • Use of builder pattern for test configuration
  • Explicit content type specification
  • Result verification through print() method

ityouknow/spring-boot-examples

spring-boot-mybatis/spring-boot-mybatis-xml/src/test/java/com/neo/web/UserControllerTest.java

            
package com.neo.web;



import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
    }

    @Test
    public void getUsers() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.post("/getUsers")
                .accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print());
    }

}