Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jsr303对引发异常的getMethod的验证_Java_Validation - Fatal编程技术网

Java jsr303对引发异常的getMethod的验证

Java jsr303对引发异常的getMethod的验证,java,validation,Java,Validation,我有以下方法 public class Invoice { public String currency="USD"; @NoNull public BigDecimal getTotal() { if ("USD".equals(this.currency)) throw new IllegalArgumentException(); else return new BigDecima

我有以下方法

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);

验证检查器崩溃的原因可能是代码中抛出的异常从未被捕获。如果您的代码周围有一个try/throw/catch块,该块专门捕获了异常并对其执行了一些操作,那么这可能会解决您的问题。

验证检查器是为捕获异常而设计的吗?验证检查器有两行,我怀疑不是。你能检查一下我的编辑吗。我在里面粘贴了验证检查代码。我不太熟悉Validator类,但我认为我的答案可能会解决你的问题。它确实解决了问题,但是它破坏了库中的其他部分,除了在正确的情况下抛出的异常。您可能希望通过分析库中的代码或通过谷歌搜索来尝试更多地了解情况。。。对不起,我帮不了你什么忙,没问题。谢谢你的时间!
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