【问题标题】:Junit : status expected <200> but it was <400>Junit:状态预期 <200> 但它是 <400>
【发布时间】:2018-05-02 11:30:51
【问题描述】:

我正在尝试为控制器级别的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());

【问题讨论】:

    标签: spring spring-boot unit-testing junit4


    【解决方案1】:

    试试下面的json:-

    String json = "{\"title\":\"Greetting\",\"value\":\"Hello world\"}";
    

    【讨论】:

    • 它可能与多个事物有关,例如换行符和空格以及最后一个额外的逗号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 2022-01-17
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多