When you test controller for a SpringBoot application, you might don't want to run a whole application, you just what to test the controller itself. So this give you possibility to do this.
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {MockServletContext.class})
@WebAppConfiguration
@ExtendWith(MockitoExtension.class)
public class TestClass {
private static MockMvc mvc;
@Mock
private YourService yourService; // the service that Controller rely on
@InjectMocks
private YourController yourController; // the class you want to test
@BeforeEach
public void beforeEach() {
mvc = MockMvcBuilders.standaloneSetup(yourController).build();
/*
the build controller alone, which means that you get rid of all filters
or intercepters that are used to build a complete application running.
Because you only want to test controller itself
*/
}
}
note: MockServletContext.class is the key point of not running whole application.
If you are using you application.class in @SpringBootTest(classes = {Application.class}) , then the whole application will run.