Mockito.verifyCompletion for spies in an asynchronous environment.
See original GitHub issueI’m writing a component test (not a unit test) against some code which is doing some asynchronous processing. In the test, I want to wait until a function is called, before checking that the state it creates is created correctly. Currently it is possible to wait until a spy is called, but it is not possible to wait until that call completes.
This is not a bug. I already have a workaround, so it’s a low-priority feature wish:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
classes = {TestConfiguration.class})
public class TestPublish {
//...
@SpyBean PublisherService spy;
@Test
public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedException {
//...
Mockito.verify(spy, Mockito.timeout(10_000).atLeast(3)).checkForPublish(eq(expected));
//Check that state created is correct.
}
}
One possible api would be to add a Mockito.verifyCompletion Another possible api would be to add a Mockito.timeoutToCompletion
Anyways: I’ve been quite happy with being able to do timeout at all, and spies are basically heaven sent, so thank you.
Best Regards, Myrle
Edit by Mockito dev: put Java code in a snippet for syntax highlighting
Issue Analytics
- State:
- Created 6 years ago
- Reactions:14
- Comments:10 (3 by maintainers)
Top GitHub Comments
Definitely would be useful to have this part of the Mockito library as a timeoutTillCompletion() style method with verify(). Please review this again and look at HadarLevi’s push above.
Bump for this issue. I’ve found the same workaround, with a utility method:
But a one-line Mockito.verifyCompletion with timeout parameters would be much more attractive.