断言抛出多个异常

Xbr*_*izh 6 java exception mockito juniper

谁能告诉我如何使用断言抛出几个异常?

\n\n

例如,这是一个类:

\n\n
 protected void checkViolation(Set<ConstraintViolation<EcritureComptable>> vViolations) throws FunctionalException {\n    if (!vViolations.isEmpty()) {\n        throw new FunctionalException("L\'\xc3\xa9criture comptable ne respecte pas les r\xc3\xa8gles de gestion.",\n                                      new ConstraintViolationException(\n                                          "L\'\xc3\xa9criture comptable ne respecte pas les contraintes de validation",\n                                          vViolations));\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

和我的测试方法:

\n\n
 @Test\nvoid checkViolation(){\n    comptabiliteManager = spy(ComptabiliteManagerImpl.class);\n    when(vViolations.isEmpty()).thenReturn(false);\n\n    assertThrows(  ConstraintViolationException.class, () ->comptabiliteManager.checkViolation(vViolations), "a string should be provided!");\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想匹配该方法并完全抛出ConstraintViolationExceptionFunctionException

\n\n

任何想法?

\n\n

谢谢

\n

JB *_*zet 5

抛出一个异常,其类型为FunctionalException。这是cause一个.FunctionalExceptionConstraintViolationException

假设assertThrowsJUnit 5 方法,它返回抛出的异常。因此,您可以简单地了解其原因并对此原因添加额外的检查。