VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-boot-mockmvc-example/

⇱ Spring Boot MockMVC Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot MockMVC Example

Last Updated : 29 Sep, 2025

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.

Project Structure:

👁 out1
Structure

pom.xml

Step 2. Main Class

  • Create WebAppMain.java to bootstrap the Spring Boot application.
  • src->main->java->com->example->mockmvcdemo->WebAppMain.java

Step 3. Create Service Class

  • Implement WelcomeService to provide business logic for greetings.
  • src->main->java->com->example->mockmvcdemo->service->WelcomeService.java

Step 4. Create Controller

  • Implement WelcomeMvcController with @GetMapping endpoints to render views and add model attributes.
  • src->main->java->com->example->mockmvcdemo->controller->WelcomeMvcController.java

Step 5. Crteate Thymeleaf template

  • Create simple HTML pages (welcome-page.html, event-page.html) to display dynamic content.
  • src->main->resources->templates->welcome->page.html
  • src->main->resources->templates->event-page.html

page.html

event-page.html

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
  • src->test->java->com->example->mockmvcdemo->WelcomeWebAppTest.java

Step 7. Faster slice test with @WebMvcTest

  • Write faster unit-style tests using @WebMvcTest and @MockBean for controller layer isolation.
  • src->test->java->com->example->mockmvcdemo->WelcomeControllerSliceTest.java

Step 8. Run Application

Start the application with mvn spring-boot:run and execute tests with mvn test or IDE run option.

mvn spring-boot:run

Testing in the Browser

http://localhost:8080/?name=Geeks

  • The / endpoint in WelcomeMvcController is called.
  • WelcomeService generates the greeting:

Welcome, Geeks to the world of programming!!!

👁 out1
output
  • welcome-page.html renders the greeting dynamically.

Running Tests Using Maven

  • Runs both integration tests (@SpringBootTest) and slice tests (@WebMvcTest).
  • MockMvc simulates HTTP requests to controller endpoints.
  • Verifies HTTP status, model attributes and view names.
  • Test results are printed in the console.

Execute all tests with:

mvn test

👁 out1
output
Comment
Article Tags:

Explore