How can I use Hibernate validator to validate the arguments inside the constructor or method? I want the validation to occur before the ValueObject creation so I can throw a
You can do like this with hibernate validator using refection to validate the arguments:
public class PersonTest {
private static ExecutableValidator executableValidator;
@BeforeClass
public static void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
executableValidator = factory.getValidator().forExecutables();
}
@Test
public void test() throws NoSuchMethodException {
Constructor constructor =
Person.class.getConstructor(String.class, String.class, int.class);
Set> violations =
executableValidator.validateConstructorParameters(constructor, new Object[]{null, "", 12});
assertEquals(2, violations.size());
}
}