java 中未使用自定义验证注释
Custom validate Annotation not used in java
我创建了自定义注释:
@Constraint(validatedBy = BeginsWithACapitalLetterValidator.class)
@Target({ FIELD, METHOD, ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface BeginsWithACapitalLetter {
String message() default "{Should begin with a capital!}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
验证者:
public class BeginsWithACapitalLetterValidator implements ConstraintValidator<BeginsWithACapitalLetter, String> {
@Override
public void initialize(BeginsWithACapitalLetter constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
String valueAsString = (String) value;
System.out.println(valueAsString);
if (valueAsString.length() > 0) {
return Character.isUpperCase(valueAsString.charAt(0));
}
return false;
}
}
我应用于单个字段:
public class Person {
@BeginsWithACapitalLetter
private String forename;
public String getForename() {
return forename;
}
public void setForename(String forename) {
this.forename = forename;
}
}
我希望使用以下方法对此进行测试:
@Test
public void nameShouldFailValidationIfFirstLetterNotCapital() {
// Given
Person person = new Person();
person.setForename("name");
// When
Set<ConstraintViolation<Object>> violations = validate(person.getForename());
// Then
assertEquals(1, violations.size());
}
private Set<ConstraintViolation<Object>> validate(Object obj) {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
return validator.validate(obj);
}
但测试失败,没有违规:
java.lang.AssertionError: expected: <1> but was: <0>
我错过了什么?
使用的进口:
'javax:javaee-api:7.0'
'org.hibernate:hibernate-validator:5.1.3.Final'
您必须验证此人而不是名字。所以而不是
Set<ConstraintViolation<Object>> violations = validate(person.getForename());
使用
Set<ConstraintViolation<Object>> violations = validate(person);
我创建了自定义注释:
@Constraint(validatedBy = BeginsWithACapitalLetterValidator.class)
@Target({ FIELD, METHOD, ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface BeginsWithACapitalLetter {
String message() default "{Should begin with a capital!}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
验证者:
public class BeginsWithACapitalLetterValidator implements ConstraintValidator<BeginsWithACapitalLetter, String> {
@Override
public void initialize(BeginsWithACapitalLetter constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
String valueAsString = (String) value;
System.out.println(valueAsString);
if (valueAsString.length() > 0) {
return Character.isUpperCase(valueAsString.charAt(0));
}
return false;
}
}
我应用于单个字段:
public class Person {
@BeginsWithACapitalLetter
private String forename;
public String getForename() {
return forename;
}
public void setForename(String forename) {
this.forename = forename;
}
}
我希望使用以下方法对此进行测试:
@Test
public void nameShouldFailValidationIfFirstLetterNotCapital() {
// Given
Person person = new Person();
person.setForename("name");
// When
Set<ConstraintViolation<Object>> violations = validate(person.getForename());
// Then
assertEquals(1, violations.size());
}
private Set<ConstraintViolation<Object>> validate(Object obj) {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
return validator.validate(obj);
}
但测试失败,没有违规:
java.lang.AssertionError: expected: <1> but was: <0>
我错过了什么?
使用的进口:
'javax:javaee-api:7.0'
'org.hibernate:hibernate-validator:5.1.3.Final'
您必须验证此人而不是名字。所以而不是
Set<ConstraintViolation<Object>> violations = validate(person.getForename());
使用
Set<ConstraintViolation<Object>> violations = validate(person);