New advertisers: 50% off your first month with code FIRSTMONTHSee slots
giuseppe-trisciuoglio avatar

Unit Test Scheduled Async

Unit-test patterns for Spring @Scheduled and @Async methods using JUnit 5, Mockito, and Awaitility.

skills.sh installs
3.9K
GitHub stars
372
last updated
Aug 21, 2026
View on GitHub

What it does

  • Covers calling @Async and @Scheduled methods directly, mocking dependencies, and verifying CompletableFuture results and execution counts
  • Includes examples for cron expression checks, retry validation, exception wrapping in ExecutionException, and Awaitility race-condition waits
  • Enforces timeouts on CompletableFuture.get() and Awaitility atMost() so tests fail instead of hanging
  • Designed for Spring Java projects testing background tasks, periodic jobs, or thread pool behavior without waiting for real schedules

Install

$ npx -y skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-scheduled-async -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

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
  2. Mock dependencies with @Mock and @InjectMocks (Mockito)
  3. Wait for completion — use CompletableFuture.get(timeout, unit) or await().atMost(...).untilAsserted(...)
  4. Call @Scheduled methods directly — do not wait for cron/fixedRate; the annotation is ignored in unit tests
  5. Test exception paths — verify ExecutionException wrapping on CompletableFuture.get()

Details

Repository
github.com/giuseppe-trisciuoglio/developer-kit
Categories
Testing & QA, Java & JVM
First seen
May 16, 2026

More from giuseppe-trisciuoglio/developer-kit

All 95

Similar to Unit Test Scheduled Async

More Testing & QA