我有以下失败的测试:
@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());
}
富如下:
import javax.validation.constraints.Size;
public class Foo {
@Size(min=9, max=9)
private String bar;
// ... public setter and getter for bar
}
我已经在 Spring MVC 的上下文之外引用了Manually call Spring Annotation Validation和Using Spring Validator,但我不确定为什么这个测试会失败。