Class ThrowableCollector
- java.lang.Object
-
- org.junit.platform.engine.support.hierarchical.ThrowableCollector
-
- Direct Known Subclasses:
OpenTest4JAwareThrowableCollector
@API(status=MAINTAINED, since="1.3") public class ThrowableCollector extends Object
Simple component that can be used to collect one or more instances ofThrowable
.This class distinguishes between
Throwables
that abort and those that fail test execution. The latter take precedence over the former, i.e. if both types ofThrowables
were collected, the ones that abort execution are reported as suppressedThrowables
of the firstThrowable
that failed execution.- Since:
- 1.3
- See Also:
OpenTest4JAwareThrowableCollector
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ThrowableCollector.Executable
Functional interface for an executable block of code that may throw aThrowable
.static interface
ThrowableCollector.Factory
Factory forThrowableCollector
instances.
-
Constructor Summary
Constructors Constructor Description ThrowableCollector(Predicate<? super Throwable> abortedExecutionPredicate)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
assertEmpty()
Assert that thisThrowableCollector
is empty (i.e., has not collected anyThrowables
).void
execute(ThrowableCollector.Executable executable)
Execute the suppliedThrowableCollector.Executable
and collect anyThrowable
thrown during the execution.Throwable
getThrowable()
Get the firstThrowable
collected by thisThrowableCollector
.boolean
isEmpty()
Determine if thisThrowableCollector
is empty (i.e., has not collected anyThrowables
).boolean
isNotEmpty()
Determine if thisThrowableCollector
is not empty (i.e., has collected at least oneThrowable
).
-
-
-
Constructor Detail
-
ThrowableCollector
public ThrowableCollector(Predicate<? super Throwable> abortedExecutionPredicate)
Create a newThrowableCollector
that uses the suppliedPredicate
to determine whether aThrowable
aborted or failed execution.- Parameters:
abortedExecutionPredicate
- the predicate used to decide whether aThrowable
aborted execution; nevernull
.
-
-
Method Detail
-
execute
public void execute(ThrowableCollector.Executable executable)
Execute the suppliedThrowableCollector.Executable
and collect anyThrowable
thrown during the execution.If the
Executable
throws a blacklisted exception — for example, anOutOfMemoryError
— this method will rethrow it.- Parameters:
executable
- theExecutable
to execute- See Also:
assertEmpty()
-
getThrowable
public Throwable getThrowable()
Get the firstThrowable
collected by thisThrowableCollector
.If this collector is not empty, the first collected
Throwable
will be returned with any additionalThrowables
suppressed in the firstThrowable
.If the first collected
Throwable
aborted execution and at least one later collectedThrowable
failed execution, the first failingThrowable
will be returned with the previous aborting and any additionalThrowables
suppressed inside.- Returns:
- the first collected
Throwable
ornull
if thisThrowableCollector
is empty - See Also:
isEmpty()
,assertEmpty()
-
isEmpty
public boolean isEmpty()
Determine if thisThrowableCollector
is empty (i.e., has not collected anyThrowables
).
-
isNotEmpty
public boolean isNotEmpty()
Determine if thisThrowableCollector
is not empty (i.e., has collected at least oneThrowable
).
-
assertEmpty
public void assertEmpty()
Assert that thisThrowableCollector
is empty (i.e., has not collected anyThrowables
).If this collector is not empty, the first collected
Throwable
will be thrown with any additionalThrowables
suppressed in the firstThrowable
. Note, however, that theThrowable
will not be wrapped. Rather, it will be masked as an unchecked exception.
-
-