spring - Junit:状态预期 <200> 但它是 <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\"}";
推荐阅读
- javascript - D3js v4更新模式多次过滤图形,无法让节点正确重置为原始数据
- django - Django:在 ajax 请求中包含一个 html
- r - 如何在R中的GA中指定决策变量的等式约束?
- python - 为什么 websocket 需要 20-30 秒才能进入 while 循环
- java - 使用 httpclient 登录网站
- azure - Azure 逻辑应用程序 - 在 SQL 中创建项目时存在触发器问题
- filter - 如何为 Power Bi 中的度量拒绝过滤器
- tailwind-css - 自动生成 Tailwind 类并将它们用作变量
- python-3.x - Numpy 数组显示的结果与用于创建它的文件不同
- csv - 来自 csvkit search mutiple 的 csvgrep