在Spring Boot中使用MockMvc测试RESTAPI的发布方法

时间:2019-03-21 22:03:39

标签: java rest spring-boot junit mockmvc

我一直收到InvalidMediaTypeException 这是我的帖子方法

PostMapping("/notes")
public Note createNote(@Valid @RequestBody Note note) {
        return noteRepository.save(note);
    }

这是我的测试方法

@Test //Testing post request 
public void TestPostMethods() throws Exception {
    Note notes = new Note();
    notes.setTitle("My Fifth Api");
    notes.setContent("Api building is fun");
    notes.setCreatedAt(new Date(21032019));
    notes.setUpdatedAt(new Date(21032019));

        RequestBuilder requestBuilder = MockMvcRequestBuilders
                .post("notes/")
                .accept(MediaType.APPLICATION_JSON_VALUE).contentType(ObjectToJson.getObjectData(notes))
                .contentType(MediaType.APPLICATION_JSON_VALUE);

        MvcResult result = mockMvc.perform(requestBuilder).andReturn();

        MockHttpServletResponse response = result.getResponse();

        assertEquals(HttpStatus.CREATED.value(), response.getStatus());


}

这是堆栈跟踪

org.springframework.http.InvalidMediaTypeException: Invalid mime type "{"title":"My Fifth Api","content":"Api building is fun","createdAt":21032019,"updatedAt":21032019}": does not contain '/'
at org.springframework.http.MediaType.parseMediaType(MediaType.java:534)
at 

0 个答案:

没有答案