【发布时间】:2016-07-13 17:15:03
【问题描述】:
我有以下测试失败:
@Test
public void testValidation() {
Validator validator = new LocalValidatorFactoryBean();
Map<String, String> map = new HashMap<String, String>();
MapBindingResult errors = new MapBindingResult(map, Foo.class.getName());
Foo foo = new Foo();
foo.setBar("ba");
validator.validate(foo, errors);
assertTrue(errors.hasFieldErrors());
}
Foo如下:
import javax.validation.constraints.Size;
public class Foo {
@Size(min=9, max=9)
private String bar;
// ... public setter and getter for bar
}
我引用了Manually call Spring Annotation Validation 和Using Spring Validator outside of the context of Spring MVC,但我不确定为什么这个测试失败了。
【问题讨论】:
-
在进行验证之前,请在
LocalValidatorFactoryBean上致电afterPropertiesSet,否则将无法正确设置。 -
那是缺失的部分。如果您使用此解决方案发布答案,我会将其标记为正确答案。我很好奇您如何确定这是解决方案(例如检查源代码、找到文档等),以便如果您在文档中找到它,我可以阅读更多相关信息。感谢您的帮助。
标签: spring hibernate-validator spring-validator