unit-test-application-events

Test Spring ApplicationEvent publishing and listener execution in unit tests.

Updated Apr 5, 2026
One-click install
npx skills add https://github.com/rizaldiem/digital-invitation-web_V2 --skill unit-test-application-events-rizaldiem
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-test-application-events
Source: https://github.com/rizaldiem/digital-invitation-web_V2/tree/main/.windsurf/skills/unit-test-application-events
Command: npx skills add https://github.com/rizaldiem/digital-invitation-web_V2 --skill unit-test-application-events-rizaldiem

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Provides patterns and practical guidance to reliably test Spring ApplicationEvent publishers and @EventListener listeners so developers can verify event-driven logic without starting a full Spring context.

Core Features & Use Cases

  • Mocked Publishing: Demonstrates mocking ApplicationEventPublisher to assert that events are published with correct payloads.
  • Listener Verification: Shows direct invocation of listener methods and verification of listener side effects against mocked dependencies.
  • Async & Conditional Handling: Covers strategies for testing asynchronous listeners (Awaitility or controlled waits) and conditional listeners with example assertions.
  • Use case: validate that creating a domain entity triggers a UserCreatedEvent, that multiple listeners react appropriately, and that asynchronous processing completes as expected.

Quick Start

Mock the ApplicationEventPublisher, perform the action that should publish the event, capture the published event with an ArgumentCaptor, and assert listener side effects or use Awaitility to await async listeners.

Frequently Asked Questions about unit-test-application-events

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test Spring ApplicationEvent publishing without starting the full application context?

Mock ApplicationEventPublisher in your unit test, trigger the action that publishes the event, and use Mockito ArgumentCaptor to capture the event payload for assertion without loading the Spring context.

What is the best way to unit test @EventListener methods in Spring Boot?

The best way to unit test @EventListener methods is to directly invoke the listener methods in your test class and verify their side effects against mocked dependencies, bypassing the Spring context entirely.

How do I test asynchronous Spring event listeners reliably in JUnit 5?

To test asynchronous Spring event listeners reliably in JUnit 5, use Awaitility or controlled waits to pause test execution until the async listener processing completes, then assert the expected side effects.

Can I verify multiple event listeners react correctly to a single published ApplicationEvent?

Yes, you can verify multi-listener propagation by capturing the published ApplicationEvent and directly invoking each listener method with the captured event, checking side effects against mocked dependencies individually.

How do I test conditional Spring event listeners that use conditional expressions?

To test conditional Spring event listeners, directly invoke the listener method with event payloads that satisfy and violate the condition, using example assertions to verify that listener logic executes only when criteria are met.