Spring Async - no data found in integration test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A common Spring integration-test failure with @Async is asserting database state before background work completes. The application works in production because time passes between request and read, but tests run too quickly and observe empty results. Reliable async testing requires explicit synchronization and correct transaction boundaries.
Why Data Is Missing in Async Tests
@Async methods run on another thread. Your test thread may call repository assertions immediately, before async insert or update commits.
A second cause is transaction scope. If the async method runs in a different thread, it does not share the caller transaction unless explicitly designed. Test-level rollback and async commit timing can create confusing outcomes.
Minimal Async Setup in Spring
First ensure async execution is enabled and executor behavior is explicit.
Then annotate service methods and return CompletableFuture when callers need completion signals.
Integration Test Pattern That Waits Correctly
In tests, wait until the async side effect is observable. Awaitility makes this readable.
This removes timing flakiness without arbitrary Thread.sleep.
Transaction and Rollback Considerations
Many integration tests use @Transactional and rollback. That can hide data from async threads or roll back before async work finishes. Prefer one of these approaches.
- Avoid test-level
@Transactionalfor async integration tests. - Use cleanup scripts or repository deletes in
@AfterEach. - If transaction management is required, make transaction boundaries explicit in service code.
If async code performs writes, use clear propagation and commit semantics.
Use this only when it matches your domain requirements.
Optional Test Profile with Synchronous Executor
Some teams keep true async behavior in a dedicated integration suite and run business-logic tests with a synchronous executor profile. This can reduce flakes while still preserving async coverage where needed.
This strategy should not replace at least one suite that verifies real threaded behavior.
Debugging Checklist
When results are missing, verify these first.
@EnableAsyncis present in active configuration.- Async method is invoked through Spring proxy, not self-invocation in same class.
- Executor bean is used as expected and has available threads.
- Assertions wait for completion condition.
- Test transaction setup does not roll back before async completion.
Thread name logging often reveals whether code actually ran on async executor.
Common Pitfalls
- Calling async method and asserting immediately.
- Using
Thread.sleepwith arbitrary delays that fail on slower CI workers. - Expecting self-invoked
@Asyncmethods to run asynchronously. - Running async integration tests inside rolled-back transactions.
- Sharing mutable state between test thread and async thread without synchronization.
Summary
- Missing data in async integration tests is usually a timing or transaction issue.
- Return
CompletableFutureor wait withAwaitilityto synchronize assertions. - Keep async executor configuration explicit and test-visible.
- Be deliberate with transaction boundaries in async write paths.
- Prefer deterministic wait conditions over fixed sleep calls.
Related reading
- Spring Async not allowing use of autowired beans
- Spring Async vs completable future run async
- Spring Async without xml config
- Spring Boot Async method in controller is executing synchronously
- Spring Autowired usage
- Spring Batch - Remote Partitioning in Manager - Worker Environment - CSV Files
- Spring boot 1.4 Testing Configuration error found multiple declarations of BootstrapWith
- Spring boot and Flyway Clear database data before integration tests

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.