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

This commit is contained in:
CaptainB 2023-05-15 15:12:29 +08:00
parent 02b2be842e
commit 14bcf14a11
1 changed files with 9 additions and 6 deletions

View File

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