jsr303验证抛出异常的getMethod

时间:2014-04-23 23:31:43

标签: java validation

我有以下方法

public class Invoice
{
    public String currency="USD";

    @NoNull
    public BigDecimal getTotal()
    {
        if ("USD".equals(this.currency))
           throw new IllegalArgumentException();
        else
           return new BigDecimal("1.00");
    }
}

当我运行它时,代码抛出一个IllegalArgumentException(),这很好。但是,当我运行验证测试时,检查程序崩溃并抛出ValidationException。我想测试继续并报告在验证测试失败时抛出异常的方法。就目前而言,任何抛出异常的方法都会使验证检查程序崩溃。

验证检查器是

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Invoice>> violations = validator.validate(this);

它抛出:

javax.validation.ValidationException: Unable to access getTotal
    at org.hibernate.validator.util.ReflectionHelper.getValue(ReflectionHelper.java:303)
    at org.hibernate.validator.metadata.MetaConstraint.getValue(MetaConstraint.java:143)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:325)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:273)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:256)
    at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:210)
    at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119)
    at com.salesfront.core.client.model.proxies.Invoice.getValidationErrors(Invoice.java:198)
    at com.salesfront.core.client.model.proxies.AccountingValidation.ValidateAccount(AccountingValidation.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.hibernate.validator.util.ReflectionHelper.getValue(ReflectionHelper.java:297)
    ... 30 more
Caused by: java.lang.IllegalArgumentException
    at com.salesfront.core.client.model.proxies.Invoice.getTotal(Invoice.java:148)
    ... 35 more

1 个答案:

答案 0 :(得分:0)

验证检查程序崩溃的原因可能是因为代码中抛出的异常永远不会被捕获。如果你用一个专门捕获异常并尝试使用它的try / throw / catch块包围你的代码,那么这可能会解决你的问题。