![]() |
VOOZH | about |
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.
The MockMvc framework provides several methods to simulate HTTP requests and validate the responses returned by Spring MVC controllers.
| Method | Purpose |
|---|---|
| perform() | Executes HTTP request |
| andExpect() | Verifies response |
| status().isOk() | Checks HTTP status |
| content().string() | Checks response body |
| jsonPath() | Verifies JSON data |
Project Structure:
Add Spring Boot Web and Test dependencies in pom.xml.
pom.xml
Create a simple Employee class with required methods.
GeekEmployee.java
Create GeekEmployeeIdGenerator.java .
GeekEmployeeIdGenerator.java
Write business logic in a service class.
GeekEmployeeService.java
The service layer handles employee data operations.
Create a Spring MVC Controller.
GeekEmployeeMvcController.java
GeekEmployeeRestController.java
Make the html list of employee.
geekemployee-list.html
Start the main class .
TestSpringmvcApplication.java
After running the spring application, our console is as follows
Output on mvc/geekemployees
Testing Part:
GeekEmployeeMvcWebTest.java
Testcase Output:
GeekEmployeeRestWebTest.java
Testcase Output: