springBoot单元测试案例

解释说明:

    1. Spring测试框架提供MockMvc对象,可以在不需要客户端-服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行Controller的请求,跟启动了测试服务器一样。

    2.perform(get(...))建立web请求。在我们的第三个用例中,通过MockMvcRequestBuilder执行GET请求。

andExpect(...)可以在perform(...)函数调用后多次调用,表示对多个条件的判断,这个函数的参数类型是ResultMatcher接口,在MockMvcResultMatchers这这个类中提供了很多返回ResultMatcher接口的工具函数。这个函数使得可以检测同一个web请求的多个方面,包括HTTP响应状态码(response status),响应的内容类型(content type),会话中存放的值,检验重定向、model或者header的内容等等。

代码如下:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Train01Application.class)
@WebAppConfiguration
public class Train01ApplicationTests {
   @Autowired
   private WebApplicationContext context;

   private MockMvc mockMvc;

   private ObjectMapper mapper=new ObjectMapper();
   @Before
   public void setupMockMvc() throws Exception{
      mockMvc= MockMvcBuilders.webAppContextSetup(context).build();
   }

   @Test
   public void testSelect() throws Exception {
      mockMvc.perform(MockMvcRequestBuilders.post("/customer/list")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
              .content("{   \"ciname\":\"\",\"cisex\":2,\"ciareacode\":\"310107\",\"ciofficename\":\"\" }"))
              .andExpect(MockMvcResultMatchers.status().isOk())
              .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
              .andDo(MockMvcResultHandlers.print());
   }
   @Test
   public void testAdd() throws Exception {
      mockMvc.perform(MockMvcRequestBuilders.post("/customer/add")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .content("{\n" +
                  "       \"ciname\":\"李四\",       \n" +
                  "       \"citelephone\":\"112121212\",   \n" +
                  "       \"cisex\":0,        \n" +
                  "       \"cibirthday\":\"19940808\",  \n" +
                  "       \"cizipcode\":\"14000\",    \n" +
                  "       \"cifax\":\"asfgafas\",    \n" +
                  "       \"ciofficename\":\"上海翰都1\",     \n" +
                  "       \"ciareafullname\":\"上海市-市辖区-普陀区\",    \n" +
                  "       \"ciaddress\":\"零陵路\",       \n" +
                  "       \"cipassword\":\"123\",     \n" +
                  "       \"ciremark\":\"新增客户功能测试5\"       \n" +
                  "}"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(MockMvcResultHandlers.print());
   }
   @Test
   public void testUpdate() throws Exception {
      mockMvc.perform(MockMvcRequestBuilders.post("/customer/update")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .content("{\n" +
                  "\t   \"ciid\":65,\n" +
                  "       \"ciname\":\"王五\",       \n" +
                  "       \"citelephone\":\"112121212\",   \n" +
                  "       \"cisex\":0,        \n" +
                  "       \"cibirthday\":\"19940808\",  \n" +
                  "       \"cizipcode\":\"14000\",    \n" +
                  "       \"cifax\":\"asfgafas\",    \n" +
                  "       \"ciofficename\":\"上海翰都1\",     \n" +
                  "       \"ciareafullname\":\"上海市-市辖区-普陀区\",    \n" +
                  "       \"ciaddress\":\"零陵路\",       \n" +
                  "       \"cipassword\":\"123\",     \n" +
                  "       \"ciremark\":\"新增客户功能单元第6次测试\"       \n" +
                  "}"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(MockMvcResultHandlers.print());
   }

}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页