比方说,我有一个这样的模型类:如何单元测试Spring MVC验证注解错误消息?
public class User {
@NotEmpty(message = "Email is required")
private String email;
}
我希望能够自定义错误消息的单元测试“通过电子邮件要求”
到目前为止,我有这样的单元测试代码哪些工作,但不检查错误消息。
@Test
public void should_respond_bad_request_with_errors_when_invalid() throws Exception
{
mock().perform(post("/registration/new"))
.andExpect(status().isBadRequest())
.andExpect(view().name("registration-form"))
.andExpect(model().attributeHasFieldErrors("user", "email"));
}
可能的重复[在Spring MVC中有基于批注的验证的单元测试形式bean的良好模式](http://stackoverflow.com/questions/9516079/good-patterns-for-unit-testing-form-beans - 即具备的,标注为基础的验证) –