Back to Repositories

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

This test suite demonstrates the implementation of Spring Boot REST API testing using MockMvc framework. It focuses on validating user-related endpoints through integration testing, ensuring proper request handling and response processing in a Spring Boot application with MyBatis XML configuration.

Test Coverage Overview

The test suite provides coverage for user data retrieval endpoints in a Spring Boot application.

Key areas covered include:
  • REST endpoint validation for /getUsers
  • HTTP POST request handling
  • JSON response processing
  • UTF-8 encoding support

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests without deploying to a server. The implementation leverages Spring Runner for test execution and employs WebApplicationContext for creating a test environment.

Notable patterns include:
  • MockMvc builder pattern for test setup
  • Spring Boot test context configuration
  • Request builder pattern for endpoint testing

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • SpringRunner test executor
  • MockMvc for HTTP request simulation
  • WebApplicationContext for Spring context setup
  • MediaType.APPLICATION_JSON_UTF8 for response formatting
  • MockMvcRequestBuilders for request construction

Best Practices Demonstrated

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

Notable practices include:
  • Proper test setup isolation using @Before
  • Clear test method naming conventions
  • Automated test context configuration
  • Structured request building
  • Response validation through print() method

ityouknow/spring-boot-examples

1.x/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());
    }

}