unit-test-application-events

Solid

Provides patterns for unit testing Spring application events. Validates event publishing with ApplicationEventPublisher, tests @EventListener annotation behavior, and verifies async event handling. Use when writing tests for event listeners, mocking application events, or verifying events were published in your Spring Boot services.

Testing & QA 278 stars 32 forks Updated 5 days ago MIT

Install

View on GitHub

Quality Score: 91/100

Stars 20%
81
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 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 3. **Capture events with ArgumentCaptor**: `ArgumentCaptor.forClass(EventType.class)` to inspect published payload 4. **Verify listener side effects**: invoke listener directly against mocked dependencies 5. **Test async handlers**: use `Thread.sleep()` or Awaitility — then assert the async operation was called 6. **Add validation checkpoints**: - After capturing an event, confirm `eventCaptor.getValue()` is not null before asserting fields - If the listener is not invoked, verify `publishEvent()` was called with the correct event type - If async assertions fail, increase wait time and check the executor pool is not saturated 7. **Cover error scenarios**: assert listeners handle exceptions ...

Details

Author
giuseppe-trisciuoglio
Repository
giuseppe-trisciuoglio/developer-kit
Created
7 months ago
Last Updated
5 days ago
Language
Python
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category