How do you test REST endpoints with MockMvc in Spring Boot?
Table of Contents
Introduction
Testing REST endpoints is a crucial part of ensuring that your Spring Boot application behaves as expected. While unit tests focus on testing isolated components, integration tests verify the interactions between different layers of the application. When testing REST APIs, you can use Spring's MockMvc
to simulate HTTP requests and validate responses without starting the full web server. This allows you to test the behavior of controllers and the underlying logic in a fast, efficient manner.
In this guide, we will walk through how to set up and use MockMvc
to test REST endpoints in a Spring Boot application.
Setting Up MockMvc for Testing
1. Add the Required Dependencies
Make sure your pom.xml
or build.gradle
file includes the necessary testing dependencies. For Maven, you can include the spring-boot-starter-test
dependency, which includes MockMvc
and other essential testing tools.
For Maven:
This starter includes JUnit, Mockito, Hamcrest, and MockMvc
, among other testing utilities.
2. Set Up MockMvc in Your Test Class
You need to autowire MockMvc
in your test class to perform HTTP requests and validate responses. The easiest way to set it up is by using @WebMvcTest
, which configures Spring Boot to load only the web layer (i.e., controllers and related components) for the test.
Explanation of Key Annotations:
-
@WebMvcTest
: This annotation tells Spring to configure only the web layer (i.e., controllers). It isolates your controller tests and does not load the full application context.MockMvc
: A mock HTTP request/response object that allows you to simulate HTTP requests and verify responses.
3. Write Tests for REST Endpoints
Once MockMvc
is set up, you can begin writing tests for your REST endpoints. Here's a breakdown of how to test different HTTP methods using MockMvc
.
Testing a GET Request
To test a GET request, use the get()
method and verify the status code and response content:
Testing a POST Request
For testing POST requests, you can use the post()
method and send a JSON payload in the request body:
Testing a PUT Request
Testing a PUT request involves sending updated data to an existing resource:
Testing a DELETE Request
To test a DELETE request, simply use delete()
:
4. Verify Response Details
You can also validate various details in the response:
- Status Code: Use
andExpect(status().isOk())
orandExpect(status().isCreated())
to check the HTTP status code. - Response Body: Use
jsonPath()
to validate the response body if the response is in JSON format. You can check individual fields or the entire structure. - Response Headers: If needed, use
andExpect(header().string("header-name", "value"))
to check specific headers.
Example: Complete Test Class
Conclusion
Testing REST endpoints with MockMvc
in Spring Boot provides a simple and effective way to simulate HTTP requests and validate responses without needing a full server environment. By using MockMvc
, you can test GET, POST, PUT, and DELETE requests, check the response status, validate the response body, and ensure that your API behaves as expected. This approach helps you maintain robust and reliable API endpoints for your Spring Boot application.
By following the steps outlined above, you can:
- Set up
MockMvc
in your test class - Write tests for various HTTP methods (GET, POST, PUT, DELETE)
- Validate response status, body, and headers