Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(import snip)
public class BeanValidationTest {
private Validator validator;
@Before
public void init() {
// バリデータを生成する
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
validator = validatorFactory.getValidator();
}
@Test
public void BeanValidationをListに設定する() {
TestForm testForm = new TestForm();
testForm.setUserIdList(Arrays.asList(123, 1234, 12345, 123456));
// バリデーションを実施
// バリデーション結果はviolationsに格納されるので、よしなに確認
Set<ConstraintViolation<TestForm>> violations = validator.validate(testForm);
assertThat(violations.size(), is(2));
}
/**
* 試験対象フォーム
*/
public class TestForm {
public TestForm() {
// 何もしない
}
@Valid
private List<@DigitsTypeUse(integer = 4, fraction = 0) Integer> userIdList;
public List<Integer> getUserIdList() {
return userIdList;
}
public void setUserIdList(List<Integer> userIdList) {
this.userIdList = userIdList;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment