Skip to content

giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-java/skills/unit-test-exception-handler

unit-test-exception-handler

Verified

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. Use when writing Spring exception handler tests, REST API error tests, or mocking controller advice.

FileKindSize
SKILL.mdskill md6.1 KB
references/examples.mdmarkdown3.1 KB

references/examples.md

View on GitHub
# Exception Handler Test Examples

## Handler with Dependencies (using mocks)

When your `@ControllerAdvice` has dependencies injected via constructor, mock them in your test:

```java
@ControllerAdvice
public class GlobalExceptionHandler {
  private final MessageService messageService;

  public GlobalExceptionHandler(MessageService messageService) {
    this.messageService = messageService;
  }

  @ExceptionHandler(BusinessException.class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  public ErrorResponse handleBusiness(BusinessException ex) {
    return new ErrorResponse(400, "Business Error", messageService.getMessage(ex.getErrorCode()));
  }
}

class HandlerWithDepsTest {
  private MockMvc mockMvc;
  private MessageService messageService = mock(MessageService.class);

  @BeforeEach
  void setUp() {
    mockMvc = MockMvcBuilders.standaloneSetup(new TestController())
        .setControllerAdvice(new GlobalExceptionHandler(messageService))
        .build();
  }

  @Test
  void shouldReturnLocalizedMessage() throws Exception {
    when(messageService.getMessage("USER_NOT_FOUND")).thenReturn("Utente non trovato");
    mockMvc.perform(get("/api/users/999"))
        .andExpect(status().isBadRequest())
        .andExpect(jsonPath("$.message").value("Utente non trovato"));
    verify(messageService).getMessage("USER_NOT_FOUND");
  }
}
```

## Testing with Spring Security Context

When your exception handler evaluates `SecurityContextHolder`:

```java
@ExceptionHandler(AuthorizationException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ErrorResponse handleAuth(AuthorizationException ex) {
  Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  return new ErrorResponse(403, "Forbidden", "Access denied for: " + auth.getName());
}

@Test
void shouldReturn403WhenNotAuthorized() throws Exception {
  SecurityContextHolder.getContext().setAuthentication(
      new UsernamePasswordAuthenticationToken("user", null, List.of())
  );
  mockMvc.perform(get("/api/admin"))
      .andExpect(status().isForbidden())
      .andExpect(jsonPath("$.message").value("Access denied for: user"));
}
```

## Testing with MessageSource (Localization)

```java
@ControllerAdvice
public class I18nExceptionHandler {
  private final MessageSource messageSource;

  public I18nExceptionHandler(MessageSource messageSource) {
    this.messageSource = messageSource;
  }

  @ExceptionHandler(ResourceNotFoundException.class)
  @ResponseStatus(HttpStatus.NOT_FOUND)
  public ErrorResponse handleNotFound(ResourceNotFoundException ex, Locale locale) {
    String message = messageSource.getMessage(ex.getMessage(), null, locale);
    return new ErrorResponse(404, "Not Found", message);
  }
}

@Test
void shouldReturnLocalizedMessageItalian() throws Exception {
  // Inject MessageSource into MockMvc
  mockMvc = MockMvcBuilders.standaloneSetup(new TestController())
      .setControllerAdvice(new I18nExceptionHandler(messageSource))
      .setLocaleResolver(() -> Locale.ITALIAN)
      .build();

  mockMvc.perform(get("/api/users/999"))
      .andExpect(status().isNotFound())
      .andExpect(jsonPath("$.message").value("Utente non trovato"));
}
```
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 319kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 319kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 319kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 197kUpdated Claude CodeCodexVerifiedInstall