Setting up the Controller to test

The controller we want to test is BasicController. The convention to create a unit test is a class name with a suffix Test. We will create a test class named BasicControllerTest.

The basic setup is shown as follows:

    public class BasicControllerTest {       private MockMvc mockMvc;       @Before       public void setup() {         this.mockMvc = MockMvcBuilders.standaloneSetup(         new BasicController())         .build();       }      }

A few important things to note are as follows:

  • mockMvc: This variable can be used across different tests. So, we define an instance variable of the MockMvc class.
  • @Before setup: This method is run before every test in order to initialize MockMvc.
  • MockMvcBuilders.standaloneSetup(new BasicController()).build(): 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.