Last active
February 21, 2017 11:22
-
-
Save kntmr/5e7e7f1637d546795fec50fe4a772aef to your computer and use it in GitHub Desktop.
Custom validator test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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