SpringBoot MockMVC

使用MockMvc,我们可以完成基于RESTful风格的SpringMVC的测试,我们可以测试完整的Spring MVC流程,即从URL请求到控制器处理,再到视图渲染都可以测试。

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc    // 使用AutoConfigureMockMvc注解
public class GirlControllerTest {

    @Autowired   // 自动装配MockMvc
    private MockMvc mockMvc;

    @Test
    public void girlList() throws Exception{
    // 模拟get请求
        mockMvc.perform(MockMvcRequestBuilders.get("/girls")).andExpect(MockMvcResultMatchers.status().is2xxSuccessful());
        // mockMvc.perform(MockMvcRequestBuilders.get("/girls")).andExpect(MockMvcResultMatchers.content().string("hello world"))
    }
}
posted @ 2019-02-24 21:01  Vincen_shen  阅读(320)  评论(0编辑  收藏  举报