unit-test-exception-handler

Test Spring Boot global exception handling with MockMvc and JUnit 5.

Updated Mar 2, 2026
One-click install
npx skills add https://github.com/MassimilianoPili/claude-code-config --skill unit-test-exception-handler-massimilianopili
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-test-exception-handler
Source: https://github.com/MassimilianoPili/claude-code-config/tree/main/skills/unit-test-exception-handler
Command: npx skills add https://github.com/MassimilianoPili/claude-code-config --skill unit-test-exception-handler-massimilianopili

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill streamlines the testing of global exception handling mechanisms in Spring Boot applications, ensuring that errors are caught, formatted, and returned with the correct HTTP status codes.

Core Features & Use Cases

  • Test @ExceptionHandler: Verify that custom exception handlers correctly map exceptions to appropriate error responses.
  • Validate HTTP Status Codes: Ensure that specific exceptions trigger the expected HTTP status codes (e.g., 404 for not found, 400 for bad request).
  • Check Error Response Formatting: Confirm that the structure and content of error responses are consistent and informative.
  • Use Case: After implementing a new @ControllerAdvice to handle UserNotFoundException, use this Skill to write tests that confirm a GET request for a non-existent user returns a 404 status and a standardized error message.

Quick Start

Use the unit-test-exception-handler skill to test the handling of a ResourceNotFoundException by making a GET request to /api/users/999 and asserting a 404 status code.

Frequently Asked Questions about unit-test-exception-handler

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test Spring Boot exception handling with MockMvc?

To test Spring Boot exception handling with MockMvc, you simulate HTTP requests that trigger exceptions and assert the resulting HTTP status codes and error response formatting. This validates that @ExceptionHandler and @ControllerAdvice implementations catch and map errors correctly.

How do I verify a custom business exception returns the correct HTTP status code in JUnit 5?

Verifying a custom business exception returns the correct HTTP status code in JUnit 5 involves triggering the exception via a MockMvc request and asserting the response status. This confirms your global exception handler accurately maps specific exceptions to expected HTTP statuses like 404 or 400.

What is the best way to validate @ControllerAdvice error response formatting?

Validating @ControllerAdvice error response formatting is best done by checking the structure and content of error responses via MockMvc. This ensures that caught exceptions produce consistent and informative error messages across your Spring Boot application.

Can I test validation errors and resource not found scenarios using Spring Boot MockMvc?

You can test validation errors and resource not found scenarios using Spring Boot MockMvc by making requests that trigger these conditions. This allows you to assert that your global exception handling returns appropriate HTTP status codes like 400 for bad requests and 404 for not found.

Why does my Spring Boot @ControllerAdvice not return the expected error response structure?

If your Spring Boot @ControllerAdvice does not return the expected error response structure, testing with MockMvc and JUnit 5 helps identify the mapping mismatch. Asserting response content confirms whether your @ExceptionHandler correctly formats the error output.