Class ExceptionCollector
ExceptionCollector is a test utility for executing code blocks,
collecting exceptions, and generating a single AssertionError
containing any exceptions encountered as suppressed exceptions.
This utility is intended to support soft assertion use cases
similar to the SoftAssertions support in AssertJ and the
assertAll() support in JUnit Jupiter.
- Since:
- 5.3.10
- Author:
- Sam Brannen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceExecutableis a functional interface that can be used to implement any generic block of code that potentially throws aThrowable. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAssert that thisExceptionCollectordoes not contain any exceptions.voidexecute(ExceptionCollector.Executable executable) Execute the suppliedExceptionCollector.Executableand track any exception thrown.Get the list of exceptions encountered inexecute(Executable).
-
Constructor Details
-
ExceptionCollector
public ExceptionCollector()
-
-
Method Details
-
execute
Execute the suppliedExceptionCollector.Executableand track any exception thrown.- Parameters:
executable- theExecutableto execute- See Also:
-
getExceptions
Get the list of exceptions encountered inexecute(Executable).- Returns:
- an unmodifiable copy of the list of exceptions, potentially empty
- See Also:
-
assertEmpty
Assert that thisExceptionCollectordoes not contain any exceptions.If this collector is empty, this method is effectively a no-op.
If this collector contains a single
ErrororException, this method rethrows the error or exception.If this collector contains a single
Throwable, this method throws anAssertionErrorwith the error message of theThrowableand with theThrowableas the cause.If this collector contains multiple exceptions, this method throws an
AssertionErrorwhose message is "Multiple Exceptions (#):" followed by a new line with the error message of each exception separated by a new line, with#replaced with the number of exceptions present. In addition, each exception will be added to theAssertionErroras asuppressed exception.- Throws:
Exception- See Also:
-