Provides patterns for unit testing `@ExceptionHandler` and `@ControllerAdvice` in Spring Boot applications. Validates error response formatting, mocks exceptions, verifies HTTP status codes, tests field-level validation errors, and asserts custom error payloads.
SkillScore100Maintained
Automatic
◆Coding pack
Last verified 8/21/2026
What it changes
Passed 22 of 22 test cases with the skill, and 20 of 22 without it.
Without skill91%
With skill100%
Checks it passed that the model alone missed
Registers the ControllerAdvice instance using setControllerAdvice
Uses MockMvcBuilders.standaloneSetup instead of loading a Spring application context
Recommends MockMvcBuilders.standaloneSetup for isolated exception handler unit testing
2 fixed20 passed either way0 still failing0 broke
Pass-rate lift
+9 pp
Tokens
+45%
Agent turns
0%
Response time
-16%
Model gemini-3.6-flashMeasured 8/21/2026v122 of 22 cases passed with skill
Tokens, turns and time describe resource use, not a dollar price.
See it in action
One of 2 test cases the skill fixed. Same model, same prompt, with and without the skill.
1 of 2
Prompt
How do I write a JUnit 5 unit test for a Spring Boot global exception handler that processes custom domain exceptions and validation errors? Please show me a complete example that includes a dummy controller and response payload verification.
What was checked, without and with the skill
Checked
Without skill
With skill
Uses MockMvcBuilders.standaloneSetup instead of loading a Spring application context
Missed
Passed
Registers the ControllerAdvice instance using setControllerAdvice
Missed
Passed
case-01 · gemini-3.6-flash · 8/21/2026 · answers are capped and redacted
What's inside
The instructions your model reads when the skill loads.
SKILL.md131 lines2 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 @ExceptionHandler methods
Testing @ControllerAdvice global 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
Create a test controller that throws specific exceptions to trigger each @ExceptionHandler
Register ControllerAdvice via setControllerAdvice() on MockMvcBuilders.standaloneSetup()
Assert HTTP status codes with .andExpect(status().isXxx())
Verify error response fields using jsonPath("$.field") matchers
Test validation errors by sending invalid payloads and checking MethodArgumentNotValidException produces field-level details
Debug failures with .andDo(print()) — if handler not invoked, verify setControllerAdvice() is called and exception type matches
Examples
Exception Handler and Error DTO
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFound(ResourceNotFoundException ex) {
return new ErrorResponse(404, "Not Found", ex.getMessage());
}
@ExceptionHandler(ValidationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)