Class ThrowableCollector
- java.lang.Object
-
- org.junit.platform.engine.support.hierarchical.ThrowableCollector
-
- Direct Known Subclasses:
OpenTest4JAndJUnit4AwareThrowableCollector,OpenTest4JAwareThrowableCollector
@API(status=MAINTAINED, since="1.3") public class ThrowableCollector extends java.lang.ObjectSimple component that can be used to collect one or more instances ofThrowable.This class distinguishes between
Throwablesthat abort and those that fail test execution. The latter take precedence over the former, i.e. if both types ofThrowableswere collected, the ones that abort execution are reported as suppressedThrowablesof the firstThrowablethat failed execution.- Since:
- 1.3
- See Also:
OpenTest4JAwareThrowableCollector
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceThrowableCollector.ExecutableFunctional interface for an executable block of code that may throw aThrowable.static interfaceThrowableCollector.FactoryFactory forThrowableCollectorinstances.
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.Predicate<? super java.lang.Throwable>abortedExecutionPredicateprivate java.lang.Throwablethrowable
-
Constructor Summary
Constructors Constructor Description ThrowableCollector(java.util.function.Predicate<? super java.lang.Throwable> abortedExecutionPredicate)Create a newThrowableCollectorthat uses the suppliedPredicateto determine whether aThrowableaborted or failed execution.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidadd(java.lang.Throwable t)Add the suppliedThrowableto thisThrowableCollector.voidassertEmpty()Assert that thisThrowableCollectoris empty (i.e., has not collected anyThrowables).voidexecute(ThrowableCollector.Executable executable)Execute the suppliedThrowableCollector.Executableand collect anyThrowablethrown during the execution.java.lang.ThrowablegetThrowable()Get the firstThrowablecollected by thisThrowableCollector.private booleanhasAbortedExecution(java.lang.Throwable t)booleanisEmpty()Determine if thisThrowableCollectoris empty (i.e., has not collected anyThrowables).booleanisNotEmpty()Determine if thisThrowableCollectoris not empty (i.e., has collected at least oneThrowable).TestExecutionResulttoTestExecutionResult()Convert the collectedThrowablesinto aTestExecutionResult.
-
-
-
Constructor Detail
-
ThrowableCollector
public ThrowableCollector(java.util.function.Predicate<? super java.lang.Throwable> abortedExecutionPredicate)
Create a newThrowableCollectorthat uses the suppliedPredicateto determine whether aThrowableaborted or failed execution.- Parameters:
abortedExecutionPredicate- the predicate used to decide whether aThrowableaborted execution; nevernull.
-
-
Method Detail
-
execute
public void execute(ThrowableCollector.Executable executable)
Execute the suppliedThrowableCollector.Executableand collect anyThrowablethrown during the execution.If the
Executablethrows an unrecoverable exception — for example, anOutOfMemoryError— this method will rethrow it.- Parameters:
executable- theExecutableto execute- See Also:
assertEmpty()
-
add
private void add(java.lang.Throwable t)
Add the suppliedThrowableto thisThrowableCollector.- Parameters:
t- theThrowableto add- See Also:
execute(Executable),assertEmpty()
-
getThrowable
public java.lang.Throwable getThrowable()
Get the firstThrowablecollected by thisThrowableCollector.If this collector is not empty, the first collected
Throwablewill be returned with any additionalThrowablessuppressed in the firstThrowable.If the first collected
Throwableaborted execution and at least one later collectedThrowablefailed execution, the first failingThrowablewill be returned with the previous aborting and any additionalThrowablessuppressed inside.- Returns:
- the first collected
Throwableornullif thisThrowableCollectoris empty - See Also:
isEmpty(),assertEmpty()
-
isEmpty
public boolean isEmpty()
Determine if thisThrowableCollectoris empty (i.e., has not collected anyThrowables).
-
isNotEmpty
public boolean isNotEmpty()
Determine if thisThrowableCollectoris not empty (i.e., has collected at least oneThrowable).
-
assertEmpty
public void assertEmpty()
Assert that thisThrowableCollectoris empty (i.e., has not collected anyThrowables).If this collector is not empty, the first collected
Throwablewill be thrown with any additionalThrowablessuppressed in the firstThrowable. Note, however, that theThrowablewill not be wrapped. Rather, it will be masked as an unchecked exception.
-
toTestExecutionResult
@API(status=MAINTAINED, since="1.6") public TestExecutionResult toTestExecutionResult()Convert the collectedThrowablesinto aTestExecutionResult.- Returns:
- aborted if the collected
Throwableaborted execution; failed if it failed execution; and successful otherwise - Since:
- 1.6
-
hasAbortedExecution
private boolean hasAbortedExecution(java.lang.Throwable t)
-
-