I have the following method
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");
}
}
The code throws an IllegalArgumentException() when I run it, which is fine. However, when I run a validation test, the checker crashes and throws a ValidationException. I would like to test to continue and report the method that throws exception as failing the validation test. As it stands, any method that throws an exception crashes the validation checker.
The validation checker is
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Invoice>> violations = validator.validate(this);
and it throws:
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
It is possible that the reason why the validation checker crashes is because the exception thrown in your code is never caught. If you surrounded your code with a try/throw/catch block that specifically caught the exception and did something with it then that might fix your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.