giuseppe-trisciuoglio avatar

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-code

Installs 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 @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

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
1giuseppe-trisciuoglio avatarlangchain4j-vector-stores-configurationgiuseppe-trisciuoglio/developer-kitProvides configuration patterns for LangChain4J vector stores in RAG applications. Use when building semantic search, integrating vector databases (PostgreSQL/p3.8Kskills.sh installs
2dpearson2699 avatardevice-integritydpearson2699/swift-ios-skillsVerify device legitimacy and app integrity using DeviceCheck (DCDevice per-device bits) and App Attest (DCAppAttestService key generation, attestation, and asse3.8Kskills.sh installs
3giuseppe-trisciuoglio avataraws-rds-spring-boot-integrationgiuseppe-trisciuoglio/developer-kitProvides patterns to configure AWS RDS (Aurora, MySQL, PostgreSQL) with Spring Boot applications. Configures HikariCP connection pools, implements read/write sp3.8Kskills.sh installs
4dotnet avatarfetch-and-send-datadotnet/skillsCall APIs, load data into components, and handle the async lifecycle in Blazor. USE FOR fetching data from a backend, submitting data to an API, displaying load3.8Kskills.sh installs
5raroque avatarvibe-securityraroque/vibe-security-skillAudits codebases for common security vulnerabilities that AI coding assistants introduce in "vibe-coded" applications. Checks for exposed API keys, broken acces3.8Kskills.sh installs
6browser-use avataropen-sourcebrowser-use/browser-useDocumentation reference for writing Python code using the browser-use open-source library. Use this skill whenever the user needs help with Agent, Browser, or T3.7Kskills.sh installs