skillfed

unit-test-application-events

This skill teaches patterns for unit testing Spring ApplicationEvent publishers and @EventListener consumers using JUnit 5 and Mockito, without booting the full Spring context. Learn to mock ApplicationEventPublisher, capture events with ArgumentCaptor, verify listener side effects, and test async event handling. Covers error scenarios and best practices for keeping event tests isolated and deterministic.

unit-test-application-events provides patterns for testing Spring ApplicationEvent publishers and listeners with JUnit 5 and Mockito.

AI-generated summary based on this skill's SKILL.md

311 37 MIT updated by giuseppe-trisciuoglio

Install

giuseppe-trisciuoglio/developer-kit/unit-test-application-events · repository language: Python

git clone https://github.com/giuseppe-trisciuoglio/developer-kit
cp -r developer-kit/plugins/developer-kit-java/skills/unit-test-application-events ~/.claude/skills/unit-test-application-events
npx skillfed install giuseppe-trisciuoglio/developer-kit/unit-test-application-events

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How to unit test Spring application events?

unit-test-application-events teaches you to test Spring ApplicationEvent publishers and listeners in isolation using JUnit 5 and Mockito without booting the full context. Mock the ApplicationEventPublisher, use ArgumentCaptor to capture published events, and verify that your @EventListener methods respond correctly. This approach keeps tests fast and deterministic by avoiding Spring's full initialization overhead.

How do I mock ApplicationEvent in Spring Boot?

unit-test-application-events covers mocking ApplicationEvent by creating test doubles of ApplicationEventPublisher with Mockito. Define your custom ApplicationEvent subclass, inject the mocked publisher into your service under test, and use ArgumentCaptor to intercept and assert on events your code publishes. This pattern isolates event logic from Spring's context and lets you verify both event creation and listener behavior.

How can I test @EventListener annotation Spring methods?

unit-test-application-events shows how to test @EventListener methods by calling them directly or publishing test events to a mocked publisher. Use ArgumentCaptor to capture events, then verify your listener's side effects—database calls, state changes, or downstream publishes. For async listeners marked @Async, configure a test executor or use CountDownLatch to synchronize and assert completion without relying on Spring's full event infrastructure.

What's the best way to verify event published in Spring service?

unit-test-application-events recommends mocking ApplicationEventPublisher and capturing events with ArgumentCaptor. Inject the mock into your service, trigger the code path that should publish, then call captor.getValue() or captor.getAllValues() to assert the event type, payload, and properties. This isolates your service test from listener implementations and lets you verify publication logic independently.

How do I test async event handler testing in Spring?

unit-test-application-events covers async event handling by configuring a test executor for @Async @EventListener methods or using synchronization primitives like CountDownLatch. Mock the publisher, trigger publication, wait for the async task to complete, then verify side effects. This ensures your async listeners behave correctly without depending on Spring's full task scheduling or requiring integration tests.

What are best practices for testing event listeners without context?

unit-test-application-events emphasizes keeping event tests isolated: mock ApplicationEventPublisher and listener dependencies, use ArgumentCaptor to verify event flow, test error scenarios separately, and avoid booting Spring. Validate that publishers emit the right events and listeners handle them correctly in unit tests; reserve integration tests for cross-component event propagation. This approach speeds up your test suite and makes event logic easier to debug.

SKILL.md

rendered from the published skill — quoted content, verbatim

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

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
plugins/developer-kit-java/skills/unit-test-application-events/SKILL.md

Related skills

Tags

event-driven-testing publisher-subscriber-patterns async-verification spring-isolation-testing mock-event-flow