Reputation: 23
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
Upvotes: 2
Views: 1789
Reputation: 242686
This problem indicates that it's not a good idea to use mock in this case.
Use real BindingResult
implementation and check its state instead:
BindingResult errors =
new BeanPropertyBindingResult(launchQueryForm, "LaunchQueryForm");
launchQueryValidator.validate(launchQueryForm, errors);
assertEquals("mismatch.LaunchQueryForm.orgId",
errors.getFieldError("orgId").getCode());
Upvotes: 1