好记性不如烂笔头,开始!以springboot集成secutiry为契机,开始坚持写博客。

项目构建工具: maven \ IDEA\ springboot \ springsecurity模块,开头就是测试用例,听说是注著名的TDD理论。

 

1、在maven的项目,src/test下,编写测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void whenQuerySuccess() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/user")
            .contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3));
    }
}

@Before注解的方法,是将应用上下文赋值到MockMvc工具中,以便后续进行restful请求测试。

 

 

 

  

  

posted on 2019-12-20 14:02  黑夜向日葵  阅读(104)  评论(0)    收藏  举报