Spring Boot event tests
Shows how to test Spring event publishers and listeners, async and after commit too, so tests catch real bugs.
What we improved
Replaced the async example, which could not prove anything: it called the listener method on a plain instance, so the code ran synchronously on the test thread and `Thread.sleep(200)` only slowed the test. The handler logic is now a plain u
Improved by YourAISkill from the original by giuseppe-trisciuoglio, under its MIT licence. The download includes the author's licence and a CHANGES.md listing every change. Both Claude reviews read the improved version.
Claude's review
8/10 Correct, runnable Spring event tests that go past basics: the publishEvent overload trap in Mockito, list-backed publisher, async thread proof with Awaitility, and after-commit listener pitfalls.
We asked Claude the question you would ask: would I be glad I installed this? Two separate reviews, the second blind to the first, must both rate a skill 7 or higher before we sell it.
In detail
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.
How you use it
- Add it to your pack, then download your pack (1 credit).
- In Claude, open Customize > Skills and upload the zip. Code execution needs to be on under Settings > Capabilities.
- Ask Claude for the job in your own words. It uses the skill when your request matches what it does. Step by step guide
Safety check
Nothing risky found: no hidden instructions, no download and run, no reading of saved passwords or keys.
An automated scan lowers risk but cannot promise zero risk. How we check skills
Preview
The first lines of SKILL.md
---
name: unit-test-application-events
description: "Patterns for testing Spring application events with JUnit 5, Mockito and AssertJ: verify that a service publishes the right event (and none when it fails), unit test @EventListener handlers by calling them directly, and prove the parts a direct call cannot reach (listener registration, @Async running on another thread, @TransactionalEventListener firing only after commit) with a small Spring context test, @RecordApplicationEvents and Awaitility instead of Thread.sleep. Covers the ApplicationEventPublisher overload trap that makes Mockito verifies pass or fail falsely. Use when writing or fixing tests for Spring event publishers or listeners, mocking ApplicationEventPublisher, or testing async or transactional event handling in Spring Boot."
allowed-tools: Read, Write, Bash, Glob, Grep
---
<!-- Modified by YourAISkill (youraiskill.com) on 2 December 2026 from the original by giuseppe-trisciuoglio. See CHANGES.md. -->
# Unit Testing Application Events
## Overview
Patterns for testing Spring event publishers and `@EventListener` consumers. Most tests need no Spring context: mock the publisher, call the listener method directly. A direct call, though, bypasses everything Spring adds around a listener (registration, `condition`, `@Async`, `@Order`, transaction phases), so each of those gets one small context test.
| What you want to prove | Test type | Tool |
|---|---|---|
| The service publishes the right event, after the right step | Unit | Mocked publisher + `ArgumentCaptor`, or a list-backed publisher |
| The service publishes nothing when it fails | Unit | `verifyNoInteractions(publisher)` |
| The handler does the right thing with the event | Unit | Call the method on a plain instance |
| The listener is registered and receives events | Context | `@SpringJUnitConfig` with only the listener and mocks |
| The handler really runs asynchronously | Context | `@EnableAsync` + Awaitility, assert the thread |
| What a whole flow publishes | Context | `@RecordApplicationEvents` + `ApplicationEvents` |
| An after-commit listener fires | Context | Commit the test transaction, or run without one |
## Instructions
1. **Add test dependencies** (below). `@ExtendWith(MockitoExtension.class)` needs `mockito-junit-jupiter`, not only `mockito-core`; `spring-boot-starter-test` brings it, along with JUnit 5, AssertJ, Awaitility and `spring-test`.
2. **Publisher tests:** mock `ApplicationEventPublisher`, capture the event with a captor typed as your event class, assert its fields, and check ordering with `InOrder` when the event must follow a save.
3. **Failure path:** make the dependency throw and assert `verifyNoInteractions(publisher)`.
4. **Listener tests:** construct the listener with mocked collaborators and call the handler method. Decide whether a failure in the handler should reach the publisher, and test that decision.
5. **Wiring tests:** for registration, `@Async` and `@TransactionalEventListener`, use a minimal Spring context and publish through the real `ApplicationEventPublisher`.
6. **Never wait with `Thread.sleep`.** Use Awaitility with a timeout, and assert on the thread to prove the handler ran asynchronously.
7. **Checkpoints when a test fails:**
- A verify on the publisher fails though the event was published: check the overload trap below.
- A context listener test never sees the call: confirm the listener class is in the test configuration (`@Import`) and that the event type matches the handler's parameter.
- An async test times out: confirm `@EnableAsync` is in the test configuration and the executor is not saturated.
- An after-commit listener never fires: the test transaction rolled back, or there was no transaction.
- A listener for a generic event (`EntityCreatedEvent<User>`) never fires: type erasure leaves Spring nothing to match the type argument against. Publish a concrete subclass (`class UserCreated extends EntityCreatedEvent<User>`) or make the event implement `ResolvableTypeProvider`. Only a context test catches this.
## Examples
...From giuseppe-trisciuoglio's repository, MIT licence.
Related skills
-
Checked
Hibernate and JPA fixes
Finds and fixes Hibernate and JPA problems like N+1 queries and lazy loading errors, so your Java app stays fast.
8/101 credit -
Checked
Spring Boot async tasks
Sets up Spring Boot @Async on bounded executors with a saturation test, so background work behaves under load.
8/101 credit -
Checked
Kotlin JPA entities
Models JPA entities in Kotlin with correct equality, fetching and relations, so you avoid N+1 and lazy errors.
7/101 credit -
Checked
Java Debugging Guide
Finds the cause of Java bugs and failing tests, and attaches a debugger safely in Maven, Gradle, Spring or Docker.
7/101 credit -
Checked
JUnit and Mockito tests
Writes and fixes Java tests with JUnit, AssertJ and Mockito that are deterministic and keep failures visible.
8/101 credit -
Checked
Modern Java Coding
Writes and reviews modern Java 21 and 25 code with records, sealed types, pattern matching and virtual threads.
7/101 credit