Imagine a case we have such dto
@CheckUserDetailsNotNull
@CheckUserMobileValid
@CheckUserEmailValid
public class UserDto {}
and, ofcourse, for each of these annotations there are dedicated constraint validators, for example for @CheckUserMobileValid there is UserMobileValidator
So, I'm gonna write a unit test for the UserMobileValidator but in scope of the test I wanna check only @CheckUserMobileValid without trigger the other validators. Here is the simple test:
public class UserMobileValidatorTest {
private Validator validator;
@Before
public void setup() {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
@Test
public void mobileShouldBeValid() {
UserDto userDto = new UserDto();
userDto.setMobile("062233442234");
Set<ConstraintViolation<UserDto>> constraintViolations = validator.validate(UserDto);
Assert.assertEquals("Expected validation error not found", 1, constraintViolations.size());
}
It works just fine but the all validators will be triggered, Is there a way to trigger only UserMobileValidator?