Automated testing plays role in building reliable applications. In Spring Boot, MockMvc allows us to test the web layer without starting the full HTTP server. With @AutoConfigureMockMvc, Spring Boot injects a MockMvc instance that we can use to simulate HTTP requests and verify responses.
What is MockMvc
Part of Spring Test, MockMvc simulates HTTP requests to Spring MVC controllers.
No real server is started; instead, it runs in-memory.
Useful for controller layer tests with full Spring context.
Step-by-Step Implementation
Step 1. Create the project
Create a project using Spring Initializr and set the
Project: Maven
Language: Java
Spring Boot: 3.x (e.g. 3.3.3)
Packaging: Jar
Java: 17
Dependencies: Spring Web, Thymeleaf, Spring Boot DevTools (optional)
Download the generated zip and unzip/import it into your IDE as an existing Maven project.
Note: If you plan to use slice tests (@WebMvcTest) you can omit templates in that setup; full-context tests with @SpringBootTest + view resolution require templates.
Step 6. Integration-style MockMvc tests
Write WelcomeWebAppTest.java using @SpringBootTest and @AutoConfigureMockMvc to test the full web layer