Create a gist now

Instantly share code, notes, and snippets.

Custom validator test
package sample;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.junit.Test;
public class SampleValidatorTest {
@Test
public void test() {
SampleBean bean = new SampleBean("foo", "bar");
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<SampleBean>> constraintViolations = validator.validate(bean);
constraintViolations.forEach(violation -> {
System.out.println(violation.getMessage() + ": [" + violation.getPropertyPath().toString() + "]");
});
}
static class SampleBean {
@SampleValidation(param = "hoge")
private String name;
@SampleValidation(param = "hoge")
private String value;
public SampleBean(String name, String value) {
this.name = name;
this.value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment