Package org.junit.runners
Class ParentRunner<T>
- java.lang.Object
-
- org.junit.runner.Runner
-
- org.junit.runners.ParentRunner<T>
-
- All Implemented Interfaces:
Describable
,Filterable
,Sortable
- Direct Known Subclasses:
BlockJUnit4ClassRunner
,Suite
public abstract class ParentRunner<T> extends Runner implements Filterable, Sortable
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 typeT
. (ForBlockJUnit4ClassRunner
,T
isMethod
. ForSuite
,T
isClass
.) 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 annotatedClassRule
s, create a compositeDescription
, and run children sequentially.- Since:
- 4.5
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Object
childrenLock
private java.util.Collection<T>
filteredChildren
private RunnerScheduler
scheduler
private TestClass
testClass
private static java.util.List<TestClassValidator>
VALIDATORS
-
Constructor Summary
Constructors Modifier Constructor Description protected
ParentRunner(java.lang.Class<?> testClass)
Constructs a newParentRunner
that will run@TestClass
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private void
applyValidators(java.util.List<java.lang.Throwable> errors)
private boolean
areAllChildrenIgnored()
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 java.util.List<TestRule>
classRules()
protected void
collectInitializationErrors(java.util.List<java.lang.Throwable> errors)
Adds toerrors
a throwable for each problem noted with the test class (available fromgetTestClass()
).private java.util.Comparator<? super T>
comparator(Sorter sorter)
protected TestClass
createTestClass(java.lang.Class<?> testClass)
protected abstract Description
describeChild(T child)
Returns aDescription
forchild
, which can be assumed to be an element of the list returned bygetChildren()
void
filter(Filter filter)
Remove tests that don't pass the parameterfilter
.protected abstract java.util.List<T>
getChildren()
Returns a list of objects that define the children of this Runner.Description
getDescription()
private java.util.Collection<T>
getFilteredChildren()
protected java.lang.String
getName()
Returns a name used to describe this Runnerprotected java.lang.annotation.Annotation[]
getRunnerAnnotations()
TestClass
getTestClass()
Returns aTestClass
object wrapping the class to be executed.protected boolean
isIgnored(T child)
Evaluates whether a child is ignored.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 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
shouldRun(Filter filter, T each)
void
sort(Sorter sorter)
Sorts the tests usingsorter
private void
validate()
private void
validateClassRules(java.util.List<java.lang.Throwable> errors)
protected void
validatePublicVoidNoArgMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, boolean isStatic, java.util.List<java.lang.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 before 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)
-
-
-
Field Detail
-
VALIDATORS
private static final java.util.List<TestClassValidator> VALIDATORS
-
childrenLock
private final java.lang.Object childrenLock
-
testClass
private final TestClass testClass
-
filteredChildren
private volatile java.util.Collection<T> filteredChildren
-
scheduler
private volatile RunnerScheduler scheduler
-
-
Constructor Detail
-
ParentRunner
protected ParentRunner(java.lang.Class<?> testClass) throws InitializationError
Constructs a newParentRunner
that will run@TestClass
- Throws:
InitializationError
-
-
Method Detail
-
createTestClass
protected TestClass createTestClass(java.lang.Class<?> testClass)
-
getChildren
protected abstract java.util.List<T> getChildren()
Returns a list of objects that define the children of this Runner.
-
describeChild
protected abstract Description describeChild(T child)
Returns aDescription
forchild
, which can be assumed to be an element of the list returned bygetChildren()
-
runChild
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()
. Subclasses are responsible for making sure that relevant test events are reported throughnotifier
-
collectInitializationErrors
protected void collectInitializationErrors(java.util.List<java.lang.Throwable> errors)
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
private void applyValidators(java.util.List<java.lang.Throwable> errors)
-
validatePublicVoidNoArgMethods
protected void validatePublicVoidNoArgMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, boolean isStatic, java.util.List<java.lang.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
private void validateClassRules(java.util.List<java.lang.Throwable> errors)
-
classBlock
protected Statement classBlock(RunNotifier notifier)
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
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.
-
withAfterClasses
protected Statement withAfterClasses(Statement statement)
Returns aStatement
: run all non-overridden@AfterClass
methods on this class and superclasses before executingstatement
; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into aMultipleFailureException
.
-
withClassRules
private Statement withClassRules(Statement statement)
- Parameters:
statement
- the base statement- Returns:
- a RunRules statement if any class-level
Rule
s are found, or the base statement
-
classRules
protected java.util.List<TestRule> classRules()
- Returns:
- the
ClassRule
s that can transform the block that runs each method in the tested class.
-
childrenInvoker
protected Statement childrenInvoker(RunNotifier notifier)
Returns aStatement
: CallrunChild(Object, RunNotifier)
on each object returned bygetChildren()
(subject to any imposed filter and sort)
-
isIgnored
protected boolean isIgnored(T child)
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
private void runChildren(RunNotifier notifier)
-
getName
protected java.lang.String getName()
Returns a name used to describe this Runner
-
getTestClass
public final TestClass getTestClass()
Returns aTestClass
object wrapping the class to be executed.
-
runLeaf
protected final void runLeaf(Statement statement, Description description, RunNotifier notifier)
Runs aStatement
that represents a leaf (aka atomic) test.
-
getRunnerAnnotations
protected java.lang.annotation.Annotation[] getRunnerAnnotations()
- Returns:
- the annotations that should be attached to this runner's description.
-
getDescription
public 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
public void run(RunNotifier notifier)
Description copied from class:Runner
Run the tests for this runner.
-
filter
public void filter(Filter filter) throws NoTestsRemainException
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
public void sort(Sorter sorter)
Description copied from interface:Sortable
Sorts the tests usingsorter
-
validate
private void validate() throws InitializationError
- Throws:
InitializationError
-
getFilteredChildren
private java.util.Collection<T> getFilteredChildren()
-
setScheduler
public void setScheduler(RunnerScheduler scheduler)
Sets a scheduler that determines the order and parallelization of children. Highly experimental feature that may change.
-
-