build: 参数校验返回demo加上断言

This commit is contained in:
CaptainB 2023-05-15 15:12:29 +08:00
parent 02b2be842e
commit 14bcf14a11

View File

@ -10,9 +10,10 @@ import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.regex.Matcher;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@ -44,10 +45,12 @@ public class UserControllerTests {
User user = new User();
user.setId("admin");
user.setName("admin");
mockMvc.perform(MockMvcRequestBuilders.post("/user/add").content(JSON.toJSONString(user)).contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk())
// .andExpect(content().contentType(MediaType.APPLICATION_JSON))
// .andExpect(jsonPath("$.person.name").value("Jason"))
mockMvc.perform(MockMvcRequestBuilders.post("/user/add")
.content(JSON.toJSONString(user))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.code").value("40000"))
.andDo(print());
}