使用MockMvc测试Controller

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wu6660563/article/details/78699032

测试类

/**
 * @author Nick
 * @version V1.0.0
 * @Date 2017/11/23 20:22
 * @description 测试类
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ProducerServiceApplication.class)
@WebAppConfiguration
public class ApplicationTests {
    @Autowired
    private WebApplicationContext context;
    private MockMvc mvc;

    /**
     * 测试Controller
     */
    @Test
    public void controllerTest() throws Exception {
        mvc = MockMvcBuilders.webAppContextSetup(context).build();
        mvc.perform(MockMvcRequestBuilders.get("/pwp/api")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .param("name", "nickwu")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("Success_nickwu")));


    }

}

猜你喜欢

转载自blog.csdn.net/wu6660563/article/details/78699032