org.springframework.test.web.servlet

Interface ResultActions



  • public interface ResultActions
    Allows applying actions, such as expectations, on the result of an executed request.

    See static factory methods in MockMvcResultMatchers and MockMvcResultHandlers.

    Since:
    3.2
    Author:
    Rossen Stoyanchev
    • Method Detail

      • andExpect

        ResultActions andExpect(ResultMatcher matcher)
                         throws java.lang.Exception
        Perform an expectation.

        Example

         static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
        
         mockMvc.perform(get("/person/1"))
           .andExpect(status().isOk())
           .andExpect(content().contentType(MediaType.APPLICATION_JSON))
           .andExpect(jsonPath("$.person.name").value("Jason"));
         

        Or alternatively provide all matchers as a vararg:

         static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
        
         mockMvc.perform(post("/form"))
           .andExpect(matchAll(
               status().isOk(),
               redirectedUrl("/person/1"),
                 model().size(1),
               model().attributeExists("person"),
               flash().attributeCount(1),
               flash().attribute("message", "success!"))
           );
         
        Throws:
        java.lang.Exception
      • andDo

        ResultActions andDo(ResultHandler handler)
                     throws java.lang.Exception
        Perform a general action.

        Example

         static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
        
         mockMvc.perform(get("/form")).andDo(print());
         
        Throws:
        java.lang.Exception
      • andReturn

        MvcResult andReturn()
        Return the result of the executed request for direct access to the results.
        Returns:
        the result of the request