How to check String in response body with mockMvc

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

I have simple integration test

@Test
public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName()          


        
12条回答
  •  时光取名叫无心
    2020-12-02 04:36

    @Sotirios Delimanolis answer do the job however I was looking for comparing strings within this mockMvc assertion

    So here it is

    .andExpect(content().string("\"Username already taken - please try with different username\""));
    

    Of course my assertion fail:

    java.lang.AssertionError: Response content expected:
    <"Username already taken - please try with different username"> but was:<"Something gone wrong">
    

    because:

      MockHttpServletResponse:
                Body = "Something gone wrong"
    

    So this is proof that it works!

提交回复
热议问题