Junit:状态预期 <200> 但它是 <400>
Junit : status expected <200> but it was <400>
我正在尝试为控制器级别的 post
请求编写测试用例。当我通过邮递员 运行 我得到 200 0k
@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\"}";
我正在尝试为控制器级别的 post
请求编写测试用例。当我通过邮递员 运行 我得到 200 0k
@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\"}";