Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mockMvc - Test Error Message

Does anybody have any tips, or does anybody know how I can test the "error message" returned by the HTTP response object?

@Autowired
private WebApplicationContext ctx;

private MockMvc mockMvc;

@Before
public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
}

Response:

MockHttpServletResponse:
              Status = 200
       Error message = null
             Headers = {Content-Type=[application/json;charset=UTF-8]}
        Content type = application/json;charset=UTF-8
like image 335
Vinchenzo Avatar asked Aug 13 '14 14:08

Vinchenzo


People also ask

What is MockMvc in Junit?

MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.

What is MockMvc test?

What is MockMvc? MockMvc is a Spring Boot test tool class that lets you test controllers without needing to start an HTTP server. In these tests the application context is loaded and you can test the web layer as if i's receiving the requests from the HTTP server without the hustle of actually starting it.


3 Answers

You can use the method status.reason().

For example:

     @Test      public void loginWithBadCredentials() {         this.mockMvc.perform(                 post("/rest/login")                         .contentType(MediaType.APPLICATION_JSON)                         .content("{\"username\": \"baduser\", \"password\": \"invalidPassword\"}")                 )                 .andDo(MockMvcResultHandlers.print())                 .andExpect(status().isUnauthorized())                 .andExpect(status().reason(containsString("Bad credentials")))                 .andExpect(unauthenticated());     }       MockHttpServletResponse:                   Status = 401            Error message = Authentication Failed: Bad credentials             Content type = null                     Body =             Forwarded URL = null           Redirected URL = null                  Cookies = [] 
like image 174
Fabricio Colombo Avatar answered Oct 14 '22 17:10

Fabricio Colombo


Even simpler:

String error =  mockMvc...     .andExpect(status().isUnauthorized())     .andReturn().getResolvedException().getMessage();  assertTrue(StringUtils.contains(error, "Bad credentials")); 
like image 38
membersound Avatar answered Oct 14 '22 17:10

membersound


This is the solution I found using JsonPath and MockMvc

this.mvc.perform(post(BASE_URL).contentType(MediaType.APPLICATION_JSON).content(responseJson)).andDo(print())
.andExpect(status().is5xxServerError())
.andExpect(jsonPath("$.message", is("There is an error while executing this test request ")));

Hope this helps.

like image 33
Akshay Shinde Avatar answered Oct 14 '22 17:10

Akshay Shinde