在测试restful风格的接口时,springmvc为我们提供了MockMVC架构,使用起来也很方便。
下面写个笔记,便于以后使用时参考备用。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import qinfeng.zheng.mockmvcdemo.dto.User;
import java.util.ArrayList;
import java.util.List;
@RestController
public class UserController {
@GetMapping("/users")
public List<User> getUserList() {
List<User> users = new ArrayList<>();
users.add(new User());
users.add(new User());
return users;
}
}
@Data
public class User {
private String username;
private String password;
}
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void getUserList() throws Exception {
// perform : 执行请求 ; MockMvcRequestBuilders.get("/url") : 构造一个get请求
mockMvc.perform(MockMvcRequestBuilders.get("/users")// 请求type : json
.contentType(MediaType.APPLICATION_JSON))
// 期望的结果状态 200
.andExpect(MockMvcResultMatchers.status().isOk())
// 期望的返回结果集合有2个元素 , $: 返回结果
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3));
}
}
@GetMapping("/getUser")
public User getUser(String username) {
User user = new User();
user.setUsername(username);
user.setPassword("123");
return user;
}
@Test
public void getUserByUsername() throws Exception {
// perform : 执行请求 ; MockMvcRequestBuilders.get("/url") : 构造一个get请求
mockMvc.perform(MockMvcRequestBuilders.get("/getUser")
.param("username","admin")
// 请求type : json
.contentType(MediaType.APPLICATION_JSON))
// 期望的结果状态 200
.andExpect(MockMvcResultMatchers.status().isOk())
// 期望的返回结果集合有2个元素 , $: 返回结果
.andExpect(MockMvcResultMatchers.jsonPath("$.password").value("123"));
}
@GetMapping("/getUser3")
public User getUserList(User user) {
System.out.println(ReflectionToStringBuilder.toString(user,ToStringStyle.MULTI_LINE_STYLE));
user.setUsername(user.getUsername()+",UK");
user.setPassword(user.getPassword()+",US");
return user;
}
@Test
public void test3() throws Exception {
// perform : 执行请求 ; MockMvcRequestBuilders.get("/url") : 构造一个get请求
mockMvc.perform(MockMvcRequestBuilders.get("/getUser3")
//springmvc会将username,password封装成一个User对象
.param("username","admin")
.param("password","123")
// 请求type : json
.contentType(MediaType.APPLICATION_JSON))
// 期望的结果状态 200
.andExpect(MockMvcResultMatchers.status().isOk())
// 期望的返回结果集合有2个元素 , $: 返回结果
.andExpect(MockMvcResultMatchers.jsonPath("$.password").value("123,US"));
}
/**
* {id:\d+} 正则表达式表明id只能是数字
* @param id
* @return
*/
@GetMapping("/user/{id:\\d+}")
public User getUserInfo(@PathVariable("id") String id) {
User user = new User();
return user;
}
@Test
public void test4() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/a")
.contentType(MediaType.APPLICATION_JSON))
// 期待返回的状态码是4XX
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}
@PostMapping("/user")
public User createUser(@RequestBody User user) {
System.out.println(user);
user.setAge(100);
return user;
}
@Test
public void testCreateUser() throws Exception {
String jsonUser = "{\"username\":\"admin\",\"password\":\"123\"}";
mockMvc.perform(MockMvcRequestBuilders.post("/user")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonUser))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.age").value("100"));
}
原文链接:https://www.cnblogs.com/z-qinfeng/p/11722783.html