上课:
public class Foo {
@NotNull
@Valid
private Bar bar;
// plus getter/setter
}
假设我要测试在设置bar
字段之后,该类现在有效。
在测试中,我正在使用Mockito获取伪造的Bar
实例。我希望此mockBar
是有效的,而不必担心Bar
可能包含的特定字段验证。
是否有Mockito方法可以使mockBar
通过验证?这是我在测试中调用验证的方式:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
assertTrue(new ArrayList<>(validator.validate(foo)).isEmpty());
javax.validation.Validator
将递归地验证foo的所有字段,因此在某些时候,验证器将对mockBar
进行操作。我需要模拟什么才能使其返回有效状态?