Given a class:
public class Foo {
@NotNull
@Valid
private Bar bar;
// plus getter/setter
}
Suppose I want to test that after the bar
field is set, the class is now valid.
In my test, I'm using Mockito to get a fake Bar
instance. I want this mockBar
to be valid, without me having to worry about the particular field validations that Bar
may contain.
Is there a Mockito way to make mockBar
pass validation? Here's how I call validation in my test:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
assertTrue(new ArrayList<>(validator.validate(foo)).isEmpty());
The javax.validation.Validator
will recursively validate all fields of foo, so at some point the validator will do something with the mockBar
. What do I need to mock to get this to return as valid?