I am trying to unit test my request handlers. I use Mockito for my unit tests.
I have a scenario like - the request(webservice) goes to request handler and the request handler dispatches a bunch of commands based on various conditions it check on the request.
I want to unit test this class, so i decided to come up with different requests and send them to this method and then verify the list of commands dispatched, ie. for request 1 it should dispatch commands A and B, for request 2 it should dispatch commands A and C etc.
How can verify the list of commands? i tried using Mockito verify,
verify(commandBus, times(1)).dispatch(new GenericCommandMessage(any(myCommand.class)));
logger.debug(“Verified commandBus.dispatch myCmd is called once”)
I get runtime error because it creates 2 different objects for GenericCommandMessage(1 object while calling the dispatch method and 1 object while checking the verify method) and reports that the 2 objects are different.
I agree to the error, i can use something like,
verify(commandBus, times(1)).dispatch(any(GenericCommandMessage.class));
logger.debug(“Verified commandBus.dispatch is called once”);
but i want to verify that this particular command(myCommand) is dispatched. Please shed some light on how to achieve this. Thanks!