Hi All,
I was experimenting with Awaitility (great framework, thanks!) and got this one result I can not understand.
The case is the following. See this code:
static int counter = 1;
@Test
public void awaitilityTest() throws Exception {
Awaitility.await().pollInterval(100, MILLISECONDS).atMost(1, SECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("trying " + counter++);
return false;
}
});
}
This works fine - I can see counter being increased and eventually the test fails. Good.
Now I wanted to verify whether it can be used Mockito (some experiments with testing of asynchronous code). It seems I can't and the following code snippet illustrates this (of course ArrayList has nothing to do with it - I simply tried to come with an example that you can execute without problems):
static int counter = 1;
@Test
public void awaitilityMockitoTest() throws Exception {
final ArrayList list = mock(ArrayList.class);
Awaitility.await().pollInterval(100, MILLISECONDS).atMost(1, SECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("trying " + counter++);
verify(list).clear();
return false;
}
});
}
When running this test I was expecting to see the same behaviour - increase of counter up to 10 and then a failure. But no, the call() method is executed only once and then the code stops. I tried to debug it but got lost in Mockito's internal methods.
Any idea why this does not work?
--
Regards,
Tomek Kaczanowski
http://practicalunittesting.com