Skip to content

Instantly share code, notes, and snippets.

@kuneo
Created October 4, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuneo/44c92ade78ae94db8cbfc0ad8b514d58 to your computer and use it in GitHub Desktop.
Save kuneo/44c92ade78ae94db8cbfc0ad8b514d58 to your computer and use it in GitHub Desktop.
(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