VOOZH about

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

⇱ Spring Boot MockMVC Testing with Example Project - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot MockMVC Testing with Example Project

Last Updated : 16 Mar, 2026

Spring Boot MockMVC is a testing framework provided by Spring that allows developers to test the web layer (controllers) of a Spring Boot application without starting the actual server. It simulates HTTP requests such as GET, POST, PUT, and DELETE and verifies the response returned by the controller.

  • Tests Spring MVC controllers without running the actual server.
  • Simulates HTTP requests and responses.
  • Helps verify status, response body, model data, and view names

Common MockMvc Methods

The MockMvc framework provides several methods to simulate HTTP requests and validate the responses returned by Spring MVC controllers.

MethodPurpose
perform()Executes HTTP request
andExpect()Verifies response
status().isOk()Checks HTTP status
content().string()Checks response body
jsonPath()Verifies JSON data

Steps to Implement MockMVC Testing

Step 1: Create a Spring Boot Project

  • Go to Spring Initializr and create a new Spring Boot project.
  • Add the dependencies.
  • Download and open the project in your IDE.

Project Structure:

👁 Project Structure
 

Add Spring Boot Web and Test dependencies in pom.xml.

pom.xml

Step 2: Create Model Class

Create a simple Employee class with required methods.

GeekEmployee.java

Step 3: Create Generator class

Create GeekEmployeeIdGenerator.java .

GeekEmployeeIdGenerator.java

Step 4: Create Service Layer

Write business logic in a service class.

GeekEmployeeService.java

The service layer handles employee data operations.

Step 5: Create Controller Layer

Create a Spring MVC Controller.

GeekEmployeeMvcController.java

GeekEmployeeRestController.java

Step 6: Create the html page.

Make the html list of employee.

geekemployee-list.html

Step 7: Run the Main class

Start the main class .

TestSpringmvcApplication.java

After running the spring application, our console is as follows

👁 Image

Output on mvc/geekemployees

👁 Image

Testing Part:

GeekEmployeeMvcWebTest.java

Testcase Output:

👁 Testcase Output

GeekEmployeeRestWebTest.java

Testcase Output:

👁 Testcase Output
Comment
Article Tags:

Explore