I have a Spring validator that contains the following code.
errors.rejectValue("myFieldName", "errors.required", new Object[] { "My Field" }, "Field required");
I want to test that the errors.required message is being picked up. I haven’t been able to find out how. What I have so far in jUnit is:
FieldError fieldError = errors.getFieldErrors().get(0);
assertEquals("myFieldName", fieldError.getField());
assertEquals("errors.required", fieldError.getCode());
//TODO: test that the message was picked up
Does anyone know how to grab the full message, with it’s arguments, etc?
I managed to find a solution.
Inside my jUnit test, I wire in the
MessageSource.Then I wrote a convenience method.
Using this method, I can find out what the configured message will be.
Although this solution doesn’t truly “get” the message from the error, it serves it’s purpose.
Hope this helps someone in the future.