Shaikh Salman
Shaikh Salman

Reputation: 57

How do I mock Handler<AsyncResult<Void>> in vertx using mockito?

interface A{
   public void verifyCredentials(JsonObject credentials, Handler<AsyncResult<Void>> handler);
}  

@RunWith(VertxUnitRunner.class)
Class C {
    protected A sampleObject;

    @BeforeClass
    public static void setup(TestContext context) {
        sampleObject = Mockito.mock(C.class);
        when(sampleObject.verifyCredentials(new JsonObject, ??)).then(false);
    }
}

How do I mock AsyncHandler using Mockito?

How can I use Argument Capture with this scenario?

Upvotes: 1

Views: 1269

Answers (1)

Alexandre Dupriez
Alexandre Dupriez

Reputation: 3036

You can use an ArgumentCaptor to capture the Handler<AsyncResult<Void>>, however, be aware that depending on which arguments are passed to the method verifyCredentials, it is likely the object capture will not be a mock. This is absolutely fine, of course, unless you explicitly want to mock the handler - and for this, the ArgumentCaptor will not help you.

So, if you want to use an ArgumentCaptor to check what handler is passed to the verifyCredentials method:

@Test
public void testIt() {
   when(sampleObject.verifyCredentials(Mockito.any(JsonObject.class), Mockito.any(Handler.class))).then(false);

   // Do your test here

   ArgumentCaptor<Handler<?>> captor = ArgumentCaptor.of(Handler.class);
   Mockito.verify(sampleObject).verifyCredentials(Mockito.any(JsonObject.class), captor.capture());

   Handler<?> handler = captor.getValue();

   // Perform assertions
}

Upvotes: 3

Related Questions