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 that takes an implementation of a functional interface in the form of a lambda and stores it for later execution.
Is there a way to verify with mockito that the dispatchMessage method of the mocking MessageHandler was called with a specific lambda expression:
Sense, 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 a statement error: The arguments are different! Required:
......Tests$$Lambda$28/ 379645464@48f278eb
The actual call has different arguments:
..........Lambda$27/ 482052083@2f217633
which makes sense since mockito is trying to compare two different functional interface implementations that have a different hash code.
So, is there any other way to verify that the dispatchMessage() method was called with a lambda that returns void and has the body method dispatcher.dispatch("activityId", "ctxId", 1l,); ?