Spring boot实践之编写接口测试用例
测试用例对开发者降低bug率,方便测试人员回归测试有十分重要的意义。
本文介绍如何使用MockMvc
编写测试用例.
在Spring boot项目中编写测试用例十分简单,通常建立一个Spring boot项目都会test目录下生成一个Test类
1 2 3 4 5 6 7
| @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Test public void contextLoads() throws Exception { } }
|
以用户查询为例,通常有一个用户实体,以及UserController
1 2 3 4 5 6 7 8 9 10
| @Data public class User {
private Long id;
private String username;
private String password; }
|
getInfo方法是一个restful接口,模拟查询用户详情
1 2 3 4 5 6 7 8 9 10 11
| @RestController @RequestMapping("user") public class UserController {
@GetMapping("info") public User getInfo(@RequestParam(name = "name", required = true) String username){ User user = new User(); user.setUsername(username + "s"); return user; } }
|
以下通过MockMvc对象,测试/user/info}
请求是否成功,并符合预期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import com.alibaba.fastjson.JSONObject; 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.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class) @SpringBootTest public class SecurityDemoApplicationTests {
@Autowired private WebApplicationContext context;
private MockMvc mockMvc;
@Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); }
@Test public void testQuery() throws Exception { String result = mockMvc.perform(get("/user/info") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("name", "jay")) .andExpect(status().isOk()) .andExpect(jsonPath("$.username").value("jays")) .andReturn().getResponse().getContentAsString(); System.out.println(result); } }
|
最终通过测试,并输出响应体
1 2 3 4 5
| { "id": 101, "username": "jays", "password": "1234" }
|
关于$.id
jsonpath的使用,参考JsonPath
同时付一段使用json参数的post请求方式,大同小异,
1 2 3 4 5
| String params = "{\"id\": 101,\"username\": \"jason\",\"password\": \"1234\"}"; mockMvc.perform(post("/user/login") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(params)) .andExpect(status().isOk());
|
注意后端接受json格式参数的方式:方法名(@RequestBody User user)