I am writing junit test for validators. Below code works:
// initialize errors
errors = createNiceMock(BindingResult.class);
errors.rejectValue("orgId", "mismatch.LaunchQueryForm.orgId");
// activate the mock
replay(errors);
// go ahead and run validate now
launchQueryValidator.validate(launchQueryForm, errors);
// verify the errors
verify(errors);
However below doesn’t work in junit – the difference here is that i am sending an additional parameter to display in the error message, Any idea how to write junit for this?
errors.rejectValue("typeInput", "mismatch.LaunchQueryForm.typeInput",
new Object[]
{ launchQueryForm.getTypeInput() }, null);
Error i get in this case is:
java.lang.AssertionError:
Expectation failure on verify:
BindingResult.rejectValue("typeInput", "mismatch.LaunchQueryForm.typeInput", ["X"], null): expected: 1, actual: 0
Thanks in advance for looking into this.
Best,
Prasad
This problem indicates that it’s not a good idea to use mock in this case.
Use real
BindingResultimplementation and check its state instead: