awaitility and mockito

2,889 views
Skip to first unread message

Tomek Kaczanowski

unread,
Jan 26, 2013, 5:19:02 PM1/26/13
to await...@googlegroups.com
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

Tomek Kaczanowski

unread,
Jan 26, 2013, 5:40:15 PM1/26/13
to await...@googlegroups.com
ah, I see it now! It is the AssertionError (http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/AssertionError.html) thrown by Mockito. It is not an Exception (but a Throwable / Error). So the thing I wanted to do is possible, but you have to catch an AssertionError like this:
@Test
    public void awaitilityTest3() 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++);
                try {
                    verify(list).clear();
                    return true;
                }
                catch(AssertionError ae) {
                    return false;
                }
            }
        });

Johan Haleby

unread,
Jan 27, 2013, 11:00:02 AM1/27/13
to await...@googlegroups.com
Glad you sorted it out.

Regards,
/Johan

--
 
 

Reply all
Reply to author
Forward
0 new messages