How to check String in response body with mockMvc

后端 未结 12 1849
遇见更好的自我
遇见更好的自我 2020-12-02 04:02

I have simple integration test

@Test
public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName()          


        
12条回答
  •  有刺的猬
    2020-12-02 04:38

    Taken from spring's tutorial

    mockMvc.perform(get("/" + userName + "/bookmarks/" 
        + this.bookmarkList.get(0).getId()))
        .andExpect(status().isOk())
        .andExpect(content().contentType(contentType))
        .andExpect(jsonPath("$.id", is(this.bookmarkList.get(0).getId().intValue())))
        .andExpect(jsonPath("$.uri", is("http://bookmark.com/1/" + userName)))
        .andExpect(jsonPath("$.description", is("A description")));
    

    is is available from import static org.hamcrest.Matchers.*;

    jsonPath is available from import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

    and jsonPath reference can be found here

提交回复
热议问题