Spring Junit:预期状态<;200>;但它是<;400>;
我正在尝试为控制器级别的Spring Junit:预期状态<;200>;但它是<;400>;,spring,spring-boot,unit-testing,junit4,Spring,Spring Boot,Unit Testing,Junit4,我正在尝试为控制器级别的post请求编写测试用例。当我跑过邮递员时,我得到了2000k @RestController @RequestMapping("/hello") public class HelloResource { @Autowired HelloService helloService; @PostMapping("/post") public Hello helloPost(@RequestBody Hello hello) {
post
请求编写测试用例。当我跑过邮递员时,我得到了2000k
@RestController
@RequestMapping("/hello")
public class HelloResource {
@Autowired
HelloService helloService;
@PostMapping("/post")
public Hello helloPost(@RequestBody Hello hello) {
return hello;
}
}
class Hello {
private String title;
private String value;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Hello(String title, String value) {
super();
this.title = title;
this.value = value;
}
public Hello() {
super();
}
}
测试用例
@Test
public void helloPost() throws Exception {
String json = "{\n" +
" \"title\":\"Greetting\", \n" +
" \"value\":\"Hello world\",\n" +
"}";
mockMvc.perform(post("/hello/post")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk());
尝试以下json方法:-
String json = "{\"title\":\"Greetting\",\"value\":\"Hello world\"}";
尝试以下json方法:-
String json = "{\"title\":\"Greetting\",\"value\":\"Hello world\"}";
它可能与多个事物相关,如新行字符和空格以及最后一个额外的逗号。它可能与多个事物相关,如新行字符和空格以及最后一个额外的逗号