I have a Spring service:
@Service
@Transactional
public class SomeService {
@Async
public void asyncMethod(Foo foo) {
// processing takes significant time
}
}
And I have an integration test for this SomeService
:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@Transactional
public class SomeServiceIntTest {
@Inject
private SomeService someService;
@Test
public void testAsyncMethod() {
Foo testData = prepareTestData();
someService.asyncMethod(testData);
verifyResults();
}
// verifyResult() with assertions, etc.
}
Here is the problem:
- as
SomeService.asyncMethod(..)
is annotated with@Async
and - as the
SpringJUnit4ClassRunner
adheres to the@Async
semantics
the testAsyncMethod
thread will fork the call someService.asyncMethod(testData)
into its own worker thread, then directly continue executing verifyResults()
, possibly before the previous worker thread has finished its work.
How can I wait for someService.asyncMethod(testData)
's completion before verifying the results? Notice that the solutions to How do I write a unit test to verify async behavior using Spring 4 and annotations? don't apply here, as someService.asyncMethod(testData)
returns void
, not a Future<?>
.