giuseppe-trisciuoglio avatar

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-code

Installs 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 @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()

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
1giuseppe-trisciuoglio avatarunit-test-utility-methodsgiuseppe-trisciuoglio/developer-kitProvides patterns for testing utility classes, static methods, and helper functions. Validates pure functions, null handling, edge cases, and boundary condition3.9Kskills.sh installs
2giuseppe-trisciuoglio avatarunit-test-exception-handlergiuseppe-trisciuoglio/developer-kitProvides patterns for unit testing `@ExceptionHandler` and `@ControllerAdvice` in Spring Boot applications. Validates error response formatting, mocks exception3.9Kskills.sh installs
3giuseppe-trisciuoglio avatarunit-test-config-propertiesgiuseppe-trisciuoglio/developer-kitProvides patterns for unit testing `@ConfigurationProperties` classes with `@ConfigurationPropertiesTest`. Validates property binding, tests validation constrai3.8Kskills.sh installs
4hyva-themes avatarhyva-playwright-testhyva-themes/hyva-ai-toolsWrite Playwright tests for Hyvä themes with Alpine.js components. This skill should be used when writing e2e tests, creating page objects, or debugging selector1.3Kskills.sh installs
5levnikolaevich avatarln-632-test-e2e-priority-auditorlevnikolaevich/claude-code-skillsAudits E2E coverage for critical user-visible journeys and wasteful E2E tests. Use when reviewing E2E journey value.381skills.sh installs
6ai-dashboad avatare2e-testingai-dashboad/flutter-skillAI-powered E2E testing for any app — Flutter, React Native, iOS, Android, Electron, Tauri, KMP, .NET MAUI. Connects via MCP to running apps so the agent can tak262skills.sh installs