I want to test the following method:
public void dispatchMessage(MessageHandler handler, String argument1, String argument2, Long argument3) {
handler.registerMessage(() -> {
dispatcher.dispatch(argument1,
argument2,
argument3);
});
}
Where MessageHandler
is a helper class which will accept a Functional Interface implementation in the form a lambda, and store it for later execution.
Is there a way to verify with mockito that the dispatchMessage
method of the mocked MessageHandler
has been called with the specific lambda expression:
Meaning, can I write such a test:
@Test
public void testDispatchMessage_Success() throws Exception {
myMessageDispatcher.dispatchMessage(handler, "activityId", "ctxId", 1l, );
verify(handler, times(1)).dispatchMessage(() -> {
dispatcher
.dispatch("activityId", "ctxId", 1l,);
});
}
}
This test will result in assertion error:
Argument(s) are different! Wanted:
......Tests$$Lambda$28/379645464@48f278eb
Actual invocation has different arguments:
..........Lambda$27/482052083@2f217633
which makes sense since mockito tries to compare two different implementations of the functional interface, which have a different hash code.
So is there some other way to verify that the method dispatchMessage()
has been called with a lambda that returns void and has a body method of
dispatcher.dispatch("activityId", "ctxId", 1l,);
?
See Question&Answers more detail:
os