mardi 20 janvier 2015

Mockito verify + any behaves unpredictable

I'm writing an integration test for my controller in Spring MVC + axon.


My controller is a simply RestController, with a method:



@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void createEventProposal(@RequestBody CreateEventProposalForm form) {
CreateEventProposalCommand command = new CreateEventProposalCommand(
new EventProposalId(),
form.getName(),
EventDescription.of(form.getDescription()),
form.getMinimalInterestThreshold());

commandGateway.send(command);
}


CreateEventProposalForm is just a value class to gather all parameters from incoming json.



EventProposalId


is another value object, representing an identifier. It could be constructed upon a string or without any parameters - in the latter case an UUID is generated.


Now, I want to write a test case, that given a proper json my controller should invoke send method on my command gateway mock with proper command object.


And this is when mockito behaves kind of unpredictable:



@Test
public void givenPostRequestShouldSendAppropriateCommandViaCommandHandler() throws Exception {

final String jsonString = asJsonString(
new CreateEventProposalForm(eventProposalName, eventDescription, minimalInterestThreshold)
);

mockMvc.perform(
post(URL_PATH)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonString)
);

verify(commandGatewayMock, times(1))
.send(
new CreateEventProposalCommand(
any(EventProposalId.class),
eventProposalName,
EventDescription.of(eventDescription),
minimalInterestThreshold
)
);


}


If I pass a new instance of EventProposalId to a EventProposalCommand constructor, say:



new CreateEventProposalCommand(
EventProposalId.of("anId"),
eventProposalName,
EventDescription.of(eventDescription),
minimalInterestThreshold
)


it fails as you would expect it. But given any(EventProposalId.class) instead, I could pass totally dummy values like



new CreateEventProposalCommand(
any(EventProposalId.class),
"dummy name",
EventDescription.of("dummy description"),
666
)


as other parameters and the test always passes.


How could I make such assertion without method parameter interception? Is this mockito's bug or should it behave this way?


Aucun commentaire:

Enregistrer un commentaire