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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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());
}
}