简体   繁体   中英

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM