
Unit Test Exception Handler
If you're writing tests for Spring's @ControllerAdvice or @ExceptionHandler methods, this walks you through the MockMvc setup that actually works. The key trick is calling setControllerAdvice() on your standalone builder, which people constantly forget and then wonder why their handler never fires. It covers the usual suspects: asserting status codes, validating JSON error payloads with jsonPath, and testing MethodArgumentNotValidException for field-level validation errors. The examples are concrete enough to copy and adapt. One nice touch is the debugging tip to chain andDo(print()) when assertions fail so you can see what's actually coming back. Saves you from guessing why your 404 handler is returning 200.
- skills.sh installs
- 3.9K
- GitHub stars
- 369
- last updated
- Aug 21, 2026
Install
$ npx -y skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-exception-handler --agent claude-codeInstalls into .claude/skills of the current project.
Files
Unit Testing ExceptionHandler and ControllerAdvice
Overview
This skill provides patterns for writing unit tests for Spring Boot exception handlers. It covers testing @ExceptionHandler methods in @ControllerAdvice classes using MockMvc, including HTTP status assertions, JSON response validation, field-level validation error testing, and mocking handler dependencies.
When to Use
- Writing unit tests for
@ExceptionHandlermethods - Testing
@ControllerAdviceglobal exception handling - Validating REST API error response formatting
- Mocking exceptions in controller tests
- Testing field-level validation error responses
- Asserting custom error payloads and HTTP status codes
Instructions
Sponsors
Details
- Repository
- github.com/giuseppe-trisciuoglio/developer-kit
- Categories
- Backend & APIs, Testing & QA, Documentation, Debugging, Java & JVM
- First seen
- May 16, 2026
More from giuseppe-trisciuoglio/developer-kit
All 102- Unit Test Mapper Converter3.9K
- Langchain4j Ai Services Patterns3.9K
- Unit Test Caching3.8K
- Unit Test Config Properties3.8K
- Unit Test Application Events3.8K
- Langchain4j Vector Stores Configuration3.8K
- Langchain4j Mcp Server Patterns3.8K
- Aws Sdk Java V2 S33.8K
- Langchain4j Testing Strategies3.8K
- Aws Sdk Java V2 Dynamodb3.8K
- Aws Rds Spring Boot Integration3.8K
- Nextjs Deployment3.8K
- Aws Sdk Java V2 Lambda3.8K
- Aws Sdk Java V2 Core3.8K
- Aws Sdk Java V2 Bedrock3.8K
- Aws Sdk Java V2 Messaging3.8K
- Aws Sdk Java V2 Secrets Manager3.8K
- Aws Sdk Java V2 Kms3.8K
- Aws Sdk Java V2 Rds3.8K
- Nestjs Drizzle Crud Generator3.8K
- Aws Lambda Python Integration3.8K
- Aws Lambda Typescript Integration3.7K
- Sonarqube Mcp3.7K
- Better Auth3.7K