unit-test-scheduled-async

Installation
Summary

Test @Scheduled and @Async methods directly without waiting for actual execution.

  • Call async and scheduled methods directly in tests instead of relying on Spring's async executor or scheduling intervals
  • Use CompletableFuture.get() with explicit timeouts to wait for async results, and Awaitility.await() for polling-based assertions on state changes
  • Mock dependencies that async methods call, then verify interactions after completion using Mockito
  • Test exception handling by catching ExecutionException from CompletableFuture.get() and verifying the underlying cause
  • Requires JUnit 5, Mockito, Awaitility, and AssertJ; avoid testing actual cron timing or thread pool behavior
SKILL.md

Unit Testing @Scheduled and @Async Methods

Overview

Patterns for unit testing Spring @Scheduled and @Async methods with JUnit 5. Test CompletableFuture results, use Awaitility for race conditions, mock scheduled task execution, and validate error handling — without waiting for real scheduling intervals.

When to Use

  • Testing @Scheduled method logic
  • Testing @Async method behavior
  • Verifying CompletableFuture results
  • Testing async error handling
  • Testing cron expression logic without waiting for actual scheduling
  • Validating thread pool behavior and execution counts
  • Testing background task logic in isolation

Instructions

  1. Call @Async methods directly — bypass Spring's async proxy; the annotation is irrelevant in unit tests
Related skills

More from giuseppe-trisciuoglio/developer-kit

Installs
872
GitHub Stars
246
First Seen
Feb 3, 2026