Mockito: wait for an invocation that matches arguments

前端 未结 2 1548
盖世英雄少女心
盖世英雄少女心 2021-01-07 17:22

I\'m writing a selenium test and verifying the server behavior with mockito. Specifically, when a button is clicked, I want to make sure the page controller calls a particul

2条回答
  •  感情败类
    2021-01-07 17:43

    This is not a super clean solution but you can do this (XX is the supposed return type here):

    final CountDownLatch latch = new CountDownLatch(1);
    
    doReturn(new Answer()
        {
            @Override
            public XX answer(InvocationOnMock invocation)
            {
                latch.countDown();
                return someInstanceOfXX;
            }
        }
    ).when(myMock).myMethod("expectedArg");
    

    Then, to test if the method is called, do:

    try {
        assertTrue(latch.await(5L, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        // Urgh... Failed. Deal with it and:
        Thread.currentThread.interrupt();
    }
    

提交回复
热议问题