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?
答案 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
}