unit-test-exception-handler

Test Spring controller exception handling with MockMvc and assert HTTP status mappings.

322|37|Updated Oct 21, 2025
One-click install
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill unit-test-exception-handler
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-test-exception-handler
Source: https://github.com/giuseppe-trisciuoglio/developer-kit/tree/main/skills/junit-test/unit-test-exception-handler
Command: npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill unit-test-exception-handler

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Ensures global error handling consistently maps exceptions to HTTP status codes and standardized error responses.

Core Features & Use Cases

  • Validate @ExceptionHandler and @ControllerAdvice error mappings.
  • Verify error response structure and HTTP status codes for different exceptions.
  • Fast, isolated tests without requiring the full application context.

Quick Start

Create MockMvc tests that exercise GlobalExceptionHandler with a simple controller that throws exceptions, asserting status and error fields.

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 exception handlers in Spring controllers with MockMvc?

Test exception handlers by creating MockMvc tests that invoke controller methods throwing exceptions, then assert HTTP status codes and error response structure. Use @ControllerAdvice with @ExceptionHandler methods to map specific exceptions to standardized error responses, validating both the mapping logic and response format in isolated unit tests.

What's the best way to verify error response formatting and status codes?

Use MockMvc to perform requests that trigger exceptions, then assert the returned HTTP status and ErrorResponse DTO fields. Test multiple exception types—ResourceNotFound, Validation, and custom exceptions—to confirm each maps to the correct status code and error message structure.

Can I test @ExceptionHandler and @ControllerAdvice without loading the full application context?

Yes. MockMvc-based unit tests validate @ExceptionHandler logic in isolation without requiring full application startup. This approach runs faster and focuses specifically on exception-to-response mappings without integration overhead.

How do I handle exception message localization in error response tests?

Include message localization logic in your GlobalExceptionHandler's @ExceptionHandler methods, then verify localized messages appear in the ErrorResponse DTO during MockMvc tests. Assert that different locales produce expected translated error messages.

What exceptions should I test in a global exception handler?

Test coverage should include ResourceNotFound, Validation, and any domain-specific custom exceptions your application defines. Each exception type should map to an appropriate HTTP status code and include relevant error details in the standardized error response.

Do I need a separate DTO for error responses?

Yes. An ErrorResponse DTO standardizes the structure of all error responses, ensuring consistent formatting across exception types. MockMvc tests verify this DTO contains required fields and correct values for each mapped exception.