我如何模拟Handler <asyncresult <void>&gt;在vertx中使用mockito?

时间:2018-03-21 00:12:16

标签: java mockito vert.x

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);
    }
}

如何使用Mockito模拟AsyncHandler?

如何在这种情况下使用Argument Capture?

1 个答案:

答案 0 :(得分:3)

您可以使用ArgumentCaptor来捕获Handler<AsyncResult<Void>>,但请注意,根据传递给方法verifyCredentials的参数,可能是对象捕获不会是模拟。当然,这绝对没问题,除非你明确想要模拟处理程序 - 为此,ArgumentCaptor对你没有帮助。

因此,如果您想使用ArgumentCaptor来检查传递给verifyCredentials方法的处理程序:

@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
}