unit-test-application-events

Installation
Summary

Testing Spring ApplicationEvent publishers and listeners with mocked dependencies and event capture patterns.

  • Mock ApplicationEventPublisher in unit tests and use ArgumentCaptor to verify published events and their data integrity
  • Test @EventListener method invocation directly by instantiating listeners and invoking handler methods with captured events
  • Handle asynchronous event processing with Thread.sleep() or Awaitility to verify async listener completion
  • Verify listener side effects on mocked dependencies and test error handling in both publishers and listeners
SKILL.md

Unit Testing Application Events

Overview

Provides actionable patterns for testing Spring ApplicationEvent publishers and @EventListener consumers using JUnit 5 and Mockito — without booting the full Spring context.

When to Use

  • Writing unit tests for event publishers or listeners
  • Verifying that an event was published with correct payload
  • Testing @EventListener method invocation and side effects
  • Testing event propagation through multiple listeners
  • Validating async event handling (@Async + @EventListener)
  • Mocking ApplicationEventPublisher in service tests

Instructions

  1. Add test dependencies: spring-boot-starter, JUnit 5, Mockito, AssertJ
  2. Mock ApplicationEventPublisher: use @Mock on the publisher field in the service under test
Related skills

More from giuseppe-trisciuoglio/developer-kit

Installs
864
GitHub Stars
246
First Seen
Feb 3, 2026