public class ExceptionCollector extends Object
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.
Modifier and Type | Class and Description |
---|---|
static interface |
ExceptionCollector.Executable
Executable is a functional interface that can be used to implement
any generic block of code that potentially throws a Throwable . |
Constructor and Description |
---|
ExceptionCollector() |
Modifier and Type | Method and Description |
---|---|
void |
assertEmpty()
Assert that this
ExceptionCollector does not contain any
exceptions. |
void |
execute(ExceptionCollector.Executable executable)
Execute the supplied
ExceptionCollector.Executable and track any exception thrown. |
List<Throwable> |
getExceptions()
Get the list of exceptions encountered in
execute(Executable) . |
public void execute(ExceptionCollector.Executable executable)
ExceptionCollector.Executable
and track any exception thrown.executable
- the Executable
to executegetExceptions()
,
assertEmpty()
public List<Throwable> getExceptions()
execute(Executable)
.assertEmpty()
public void assertEmpty() throws Exception
ExceptionCollector
does not contain any
exceptions.
If this collector is empty, this method is effectively a no-op.
If this collector contains a single Error
or Exception
,
this method rethrows the error or exception.
If this collector contains a single Throwable
, this method throws
an AssertionError
with the error message of the Throwable
and with the Throwable
as the cause.
If this collector contains multiple exceptions, this method throws an
AssertionError
whose 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 the AssertionError
as
a suppressed exception
.
Exception
execute(Executable)
,
getExceptions()