Created
October 4, 2016 14:06
-
-
Save kuneo/44c92ade78ae94db8cbfc0ad8b514d58 to your computer and use it in GitHub Desktop.
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
(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