Writing the Test method

The complete Test method is shown in the following code:

    @Test     public void basicTest() throws Exception {       this.mockMvc       .perform(       get("/welcome")       .accept(MediaType.parseMediaType       ("application/html;charset=UTF-8")))       .andExpect(status().isOk())       .andExpect( content().contentType       ("application/html;charset=UTF-8"))       .andExpect(content().        string("Welcome to Spring MVC"));     }

A few important things to note are as follows:

  • MockMvc mockMvc.perform: This method executes the request and returns an instance of ResultActions that allows chaining calls. In this example, we are chaining the andExpect calls to check expectations.
  • get("/welcome").accept(MediaType.parseMediaType("application/html;charset=UTF-8")): This ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.