Package org.junit.runners
Class ParentRunner<T>
java.lang.Object
org.junit.runner.Runner
org.junit.runners.ParentRunner<T>
- All Implemented Interfaces:
Describable
,Filterable
,Orderable
,Sortable
- Direct Known Subclasses:
BlockJUnit4ClassRunner
,Suite
Provides most of the functionality specific to a Runner that implements a
"parent node" in the test tree, with children defined by objects of some data
type
T
. (For BlockJUnit4ClassRunner
, T
is
Method
. For Suite
, T
is Class
.) Subclasses
must implement finding the children of the node, describing each child, and
running each child. ParentRunner will filter and sort children, handle
@BeforeClass
and @AfterClass
methods,
handle annotated ClassRule
s, create a composite
Description
, and run children sequentially.- Since:
- 4.5
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Lock
private RunnerScheduler
private final TestClass
private static final List
<TestClassValidator> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ParentRunner
(Class<?> testClass) Constructs a newParentRunner
that will run@TestClass
protected
ParentRunner
(TestClass testClass) Constructs a newParentRunner
that will run theTestClass
. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
applyValidators
(List<Throwable> errors) private boolean
protected Statement
childrenInvoker
(RunNotifier notifier) Returns aStatement
: CallrunChild(Object, RunNotifier)
on each object returned bygetChildren()
(subject to any imposed filter and sort)protected Statement
classBlock
(RunNotifier notifier) Constructs aStatement
to run all of the tests in the test class.protected void
collectInitializationErrors
(List<Throwable> errors) Adds toerrors
a throwable for each problem noted with the test class (available fromgetTestClass()
).private Comparator
<? super T> comparator
(Sorter sorter) protected TestClass
createTestClass
(Class<?> testClass) Deprecated.protected abstract Description
describeChild
(T child) Returns aDescription
forchild
, which can be assumed to be an element of the list returned bygetChildren()
void
Remove tests that don't pass the parameterfilter
.Returns a list of objects that define the children of this Runner.protected String
getName()
Returns a name used to describe this Runnerprotected Annotation[]
final TestClass
Returns aTestClass
object wrapping the class to be executed.protected boolean
Evaluates whether a child is ignored.void
Implementation ofOrderable.order(Orderer)
.void
run
(RunNotifier notifier) Run the tests for this runner.protected abstract void
runChild
(T child, RunNotifier notifier) Runs the test corresponding tochild
, which can be assumed to be an element of the list returned bygetChildren()
.private void
runChildren
(RunNotifier notifier) protected final void
runLeaf
(Statement statement, Description description, RunNotifier notifier) Runs aStatement
that represents a leaf (aka atomic) test.void
setScheduler
(RunnerScheduler scheduler) Sets a scheduler that determines the order and parallelization of children.private boolean
private boolean
void
Sorts the tests usingsorter
private void
validate()
private void
validateClassRules
(List<Throwable> errors) protected void
validatePublicVoidNoArgMethods
(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) Adds toerrors
if any method in this class is annotated withannotation
, but: is not public, or takes parameters, or returns something other than void, or is static (givenisStatic is false
), or is not static (givenisStatic is true
).protected Statement
withAfterClasses
(Statement statement) Returns aStatement
: run all non-overridden@AfterClass
methods on this class and superclasses after executingstatement
; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into aMultipleFailureException
.protected Statement
withBeforeClasses
(Statement statement) Returns aStatement
: run all non-overridden@BeforeClass
methods on this class and superclasses before executingstatement
; if any throws an Exception, stop execution and pass the exception on.private Statement
withClassRules
(Statement statement) protected final Statement
withInterruptIsolation
(Statement statement)
-
Field Details
-
VALIDATORS
-
childrenLock
-
testClass
-
filteredChildren
-
scheduler
-
-
Constructor Details
-
ParentRunner
Constructs a newParentRunner
that will run@TestClass
- Throws:
InitializationError
-
ParentRunner
Constructs a newParentRunner
that will run theTestClass
.- Throws:
InitializationError
- Since:
- 4.13
-
-
Method Details
-
createTestClass
Deprecated.Please useParentRunner(org.junit.runners.model.TestClass)
.- Since:
- 4.12
-
getChildren
Returns a list of objects that define the children of this Runner. -
describeChild
Returns aDescription
forchild
, which can be assumed to be an element of the list returned bygetChildren()
-
runChild
Runs the test corresponding tochild
, which can be assumed to be an element of the list returned bygetChildren()
. Subclasses are responsible for making sure that relevant test events are reported throughnotifier
-
collectInitializationErrors
Adds toerrors
a throwable for each problem noted with the test class (available fromgetTestClass()
). Default implementation adds an error for each method annotated with@BeforeClass
or@AfterClass
that is notpublic static void
with no arguments. -
applyValidators
-
validatePublicVoidNoArgMethods
protected void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) Adds toerrors
if any method in this class is annotated withannotation
, but:- is not public, or
- takes parameters, or
- returns something other than void, or
- is static (given
isStatic is false
), or - is not static (given
isStatic is true
).
-
validateClassRules
-
classBlock
Constructs aStatement
to run all of the tests in the test class. Override to add pre-/post-processing. Here is an outline of the implementation:- Determine the children to be run using
getChildren()
(subject to any imposed filter and sort). - If there are any children remaining after filtering and ignoring,
construct a statement that will:
- Apply all
ClassRule
s on the test-class and superclasses. - Run all non-overridden
@BeforeClass
methods on the test-class and superclasses; if any throws an Exception, stop execution and pass the exception on. - Run all remaining tests on the test-class.
- Run all non-overridden
@AfterClass
methods on the test-class and superclasses: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into aMultipleFailureException
.
- Apply all
- Returns:
Statement
- Determine the children to be run using
-
areAllChildrenIgnored
private boolean areAllChildrenIgnored() -
withBeforeClasses
Returns aStatement
: run all non-overridden@BeforeClass
methods on this class and superclasses before executingstatement
; if any throws an Exception, stop execution and pass the exception on. -
withAfterClasses
Returns aStatement
: run all non-overridden@AfterClass
methods on this class and superclasses after executingstatement
; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into aMultipleFailureException
. -
withClassRules
- Parameters:
statement
- the base statement- Returns:
- a RunRules statement if any class-level
Rule
s are found, or the base statement
-
classRules
- Returns:
- the
ClassRule
s that can transform the block that runs each method in the tested class.
-
childrenInvoker
Returns aStatement
: CallrunChild(Object, RunNotifier)
on each object returned bygetChildren()
(subject to any imposed filter and sort) -
withInterruptIsolation
- Returns:
- a
Statement
: clears interrupt status of current thread after execution of statement
-
isIgnored
Evaluates whether a child is ignored. The default implementation always returnsfalse
.BlockJUnit4ClassRunner
, for example, overrides this method to filter tests based on theIgnore
annotation. -
runChildren
-
getName
Returns a name used to describe this Runner -
getTestClass
Returns aTestClass
object wrapping the class to be executed. -
runLeaf
Runs aStatement
that represents a leaf (aka atomic) test. -
getRunnerAnnotations
- Returns:
- the annotations that should be attached to this runner's description.
-
getDescription
- Specified by:
getDescription
in interfaceDescribable
- Specified by:
getDescription
in classRunner
- Returns:
- a
Description
showing the tests to be run by the receiver
-
run
Description copied from class:Runner
Run the tests for this runner. -
filter
Description copied from interface:Filterable
Remove tests that don't pass the parameterfilter
.- Specified by:
filter
in interfaceFilterable
- Parameters:
filter
- theFilter
to apply- Throws:
NoTestsRemainException
- if all tests are filtered out
-
sort
Description copied from interface:Sortable
Sorts the tests usingsorter
-
order
Implementation ofOrderable.order(Orderer)
.- Specified by:
order
in interfaceOrderable
- Throws:
InvalidOrderingException
- if orderer does something invalid (like remove or add children)- Since:
- 4.13
-
shouldNotReorder
private boolean shouldNotReorder() -
validate
- Throws:
InitializationError
-
getFilteredChildren
-
shouldRun
-
comparator
-
setScheduler
Sets a scheduler that determines the order and parallelization of children. Highly experimental feature that may change.
-
ParentRunner(org.junit.runners.model.TestClass)
.