How do you test error responses in REST APIs with Spring Boot?

Table of Contents

Introduction

Testing error responses in REST APIs is a crucial part of ensuring that your application behaves as expected during failure scenarios. Spring Boot provides several tools, such as MockMvc and TestRestTemplate, to simulate requests that result in errors, making it easier to test and validate error handling mechanisms. In this guide, we will cover how to test error responses and ensure your REST APIs handle errors correctly.

Testing Error Responses Using MockMvc

1. Setting Up MockMvc for Tests

MockMvc is a powerful tool in Spring Boot that allows you to test your REST controllers without starting a full HTTP server. It simulates HTTP requests and responses, making it ideal for testing error scenarios. You can use MockMvc to trigger endpoints that throw exceptions and verify that the correct error responses are returned.

Example: Testing 404 Not Found Error

Suppose you have a controller that throws a ResourceNotFoundException when a requested resource is not found:

In your test, you can simulate a request to a non-existent user and verify the error response:

In this example:

  • The request to /users/999 simulates fetching a user that does not exist.
  • The status().isNotFound() asserts that the response status code is 404.
  • The jsonPath("$.message") checks if the error message in the response body matches the expected error message.

2. Testing Custom Error Responses

You can define custom error responses using @ControllerAdvice to handle exceptions globally and return a structured error response. Here's how to implement it:

Global Exception Handler:

The ErrorResponse class:

Testing the Custom Error Response:

Testing Error Responses Using TestRestTemplate

TestRestTemplate can also be used to test error responses for REST APIs. Unlike MockMvc, which runs in the test environment, TestRestTemplate works like a real client, making actual HTTP calls to your Spring Boot application.

Example: Testing 400 Bad Request

Suppose you have a controller that validates the input data for a POST request and returns a 400 Bad Request error when validation fails:

In your test, you can simulate a POST request with invalid data and check for a 400 Bad Request response:

In this example:

  • A POST request is made with invalid user data (empty fields).
  • The response status is asserted to be 400 Bad Request.
  • The response body contains the validation error message.

Conclusion

Testing error responses in Spring Boot is essential to ensure that your application handles failure scenarios gracefully. You can use tools like MockMvc for testing REST controllers directly and TestRestTemplate for simulating client-side requests. By testing various error scenarios, such as resource not found (404), bad requests (400), and custom error responses, you ensure that your API behaves as expected and provides meaningful feedback to clients when errors occur.

Similar Questions