unit-test-exception-handler_skill

This skill helps you validate exception handling in Spring controllers by testing @ExceptionHandler and @ControllerAdvice with MockMvc for precise error
  • Python

99

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill giuseppe-trisciuoglio/developer-kit --skill unit-test-exception-handler

  • SKILL.md15.7 KB

Overview

This skill provides pragmatic patterns for unit testing Spring @ExceptionHandler methods and @ControllerAdvice classes using MockMvc. It focuses on verifying exception-to-error-response transformations, HTTP status codes, and error payload shape without running full integration tests. The goal is fast, maintainable tests that validate error formatting, validation details, and side effects like logging or localization.

How this skill works

Create lightweight test controllers that deliberately throw exceptions and register your ControllerAdvice with MockMvcBuilders.standaloneSetup(...).setControllerAdvice(...). Build targeted tests that perform MockMvc requests and assert status codes and JSON response fields. You can inject mocked services into the handler to verify localization, logging, or other contextual behavior while keeping tests small and focused.

When to use it

  • Validating that @ExceptionHandler methods map exceptions to correct HTTP status codes
  • Checking error response shape and required fields (timestamp, status, error, message)
  • Testing validation error handling (MethodArgumentNotValidException) and field-level messages
  • Verifying logging, localization, or other side effects triggered by exception handlers
  • Writing fast unit tests that avoid full application context or integration test overhead

Best practices

  • Use simple test controllers that throw specific exceptions to exercise each handler
  • Register your ControllerAdvice explicitly with MockMvc standaloneSetup to isolate behavior
  • Assert both HTTP status and JSON payload fields, including nested validation errors
  • Mock dependent services (message sources, logging) to verify side effects without external systems
  • Test each exception type, plus a generic fallback exception handler, to avoid gaps

Example use cases

  • Unit test that ResourceNotFoundException returns 404 and expected error message
  • Verify MethodArgumentNotValidException produces an errors map with per-field messages
  • Assert DuplicateResourceException maps to 409 Conflict and contains conflict details
  • Mock a MessageService to ensure business exceptions return localized messages
  • Confirm LoggingService.logException is invoked when a BusinessException is handled

FAQ

No. Use MockMvcBuilders.standaloneSetup with a test controller and setControllerAdvice to exercise handlers without starting the full context.

How do I test validation errors with field-level messages?

Trigger MethodArgumentNotValidException by POSTing invalid JSON to a controller with @Valid and assert jsonPath($.errors.fieldName) values.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
unit-test-exception-handler skill by giuseppe-trisciuoglio/developer-kit | VeilStrat