Junit:预期状态< 200>但它是< 400>

时间:2018-05-02 11:30:51

标签: spring unit-testing junit4

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

1 个答案:

答案 0 :(得分:1)

尝试下面的json: -

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