
Unit Test Scheduled Async
This gives you patterns for unit testing Spring's @Scheduled and @Async methods without waiting for actual cron intervals or thread pool execution. It shows how to call scheduled methods directly (the annotations are ignored in unit tests anyway), use CompletableFuture.get() with timeouts, and leverage Awaitility for race conditions. The examples cover mocking dependencies with Mockito, testing exception propagation through ExecutionException, and verifying execution counts. The honest take: most developers test these wrong by either trying to invoke the Spring proxy or waiting for real scheduling, so having clear examples of calling methods directly and mocking the underlying logic saves a lot of confusion.
- skills.sh installs
- 3.9K
- GitHub stars
- 369
- last updated
- Aug 21, 2026
Install
$ npx -y skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-scheduled-async --agent claude-codeInstalls into .claude/skills of the current project.
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
@Scheduledmethod logic - Testing
@Asyncmethod behavior - Verifying
CompletableFutureresults - 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
- Call
@Asyncmethods directly — bypass Spring's async proxy; the annotation is irrelevant in unit tests - Mock dependencies with
@Mockand@InjectMocks(Mockito) - Wait for completion — use
CompletableFuture.get(timeout, unit)orawait().atMost(...).untilAsserted(...) - Call
@Scheduledmethods directly — do not wait for cron/fixedRate; the annotation is ignored in unit tests - Test exception paths — verify
ExecutionExceptionwrapping onCompletableFuture.get()
Sponsors
Details
- Repository
- github.com/giuseppe-trisciuoglio/developer-kit
- Categories
- Testing & QA, Automation & Workflows, Java & JVM
- First seen
- May 16, 2026
More from giuseppe-trisciuoglio/developer-kit
All 102- Rag3.9K
- Unit Test Utility Methods3.9K
- Qdrant3.9K
- Unit Test Exception Handler3.9K
- Unit Test Mapper Converter3.9K
- Langchain4j Ai Services Patterns3.9K
- Unit Test Caching3.8K
- Unit Test Config Properties3.8K
- Unit Test Application Events3.8K
- Langchain4j Vector Stores Configuration3.8K
- Langchain4j Mcp Server Patterns3.8K
- Aws Sdk Java V2 S33.8K
- Langchain4j Testing Strategies3.8K
- Aws Sdk Java V2 Dynamodb3.8K
- Aws Rds Spring Boot Integration3.8K
- Nextjs Deployment3.8K
- Aws Sdk Java V2 Lambda3.8K
- Aws Sdk Java V2 Core3.8K
- Aws Sdk Java V2 Bedrock3.8K
- Aws Sdk Java V2 Messaging3.8K
- Aws Sdk Java V2 Secrets Manager3.8K
- Aws Sdk Java V2 Kms3.8K
- Aws Sdk Java V2 Rds3.8K
- Nestjs Drizzle Crud Generator3.8K