使用' org.apache.bval.bundle-1.1.1.jar'提供的bean验证实现时,我遇到了一些非常奇怪的行为。
通过以下单元测试:
package testing;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.bval.jsr.ApacheValidationProvider;
import org.junit.Before;
import org.junit.Test;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.validation.*;
import javax.validation.constraints.AssertFalse;
public class ValidationTestingTest {
interface Marker {}
class HasValidation {
private List<String> holder = new LinkedList<String>();
@SystemErrorInfo(errorNumber = 33)
@AssertFalse(groups = Marker.class)
public boolean isServiceAgreementCurrencyDifferentToBankAccount() {
return false;
}
@SystemErrorInfo(errorNumber = 22)
@AssertFalse(groups = Marker.class)
public boolean isTooManyReconciliationTotalsFound() {
boolean result = (holder.size() > 1);
return result;
}
@SystemErrorInfo(errorNumber = 11)
@AssertFalse(groups = Marker.class)
public boolean isInsufficientAvailableFunds() {
return false;
}
public List<String> getHolder() {
return holder;
}
public void setHolder(List<String> holder) {
this.holder = holder;
}
public void addToHolder(String s) {
holder.add(s);
}
}
@Test
public void test() {
HasValidation h = new HasValidation();
h.addToHolder("ssss");
h.addToHolder("ssss2");
Validator beanValidator = getValidator();
Set<ConstraintViolation<HasValidation>> violationSet = beanValidator.validate(h, Marker.class);
for (ConstraintViolation<HasValidation> constraintViolation : violationSet) {
System.out.println(constraintViolation);
}
assertEquals(1, violationSet.size());
assertTrue(violationSet.iterator().next().getPropertyPath().toString().contains("tooManyReconciliationTotalsFound"));
}
private Validator getValidator() {
Configuration<?> configuration = Validation.byProvider(ApacheValidationProvider.class).configure();
ValidatorFactory myfactory = configuration.buildValidatorFactory();
Validator beanValidator = myfactory.getValidator();
return beanValidator;
}
}
如果我反复运行它,偶尔测试将失败并显示错误的错误消息。我很确定这是实现本身的一个错误,而不是我的代码中的错误,但是我在执行验证时并不过分熟悉组的使用,因此可能就是这样。 有没有人知道测试为什么会间歇性地失败,或者它是否可能是一个错误,所以我应该这样报告?
如果我将提供程序切换到&#39; hibernate-validator-5.2.4.Final.jar&#39;提供的Hibernate验证器。那么测试每次都会通过。