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
public abstract class ParentRunner<T> extends Runner implements Filterable, Orderable
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
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ParentRunner(java.lang.Class<?> testClass)
Constructs a newParentRunner
that will run@TestClass
protected
ParentRunner(TestClass testClass)
Constructs a newParentRunner
that will run theTestClass
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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()
).protected TestClass
createTestClass(java.lang.Class<?> testClass)
Deprecated.Please useParentRunner(org.junit.runners.model.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()
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
order(Orderer orderer)
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()
.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.void
sort(Sorter sorter)
Sorts the tests usingsorter
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 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.protected Statement
withInterruptIsolation(Statement statement)
-
-
-
Constructor Detail
-
ParentRunner
protected ParentRunner(java.lang.Class<?> testClass) throws InitializationError
Constructs a newParentRunner
that will run@TestClass
- Throws:
InitializationError
-
ParentRunner
protected ParentRunner(TestClass testClass) throws InitializationError
Constructs a newParentRunner
that will run theTestClass
.- Throws:
InitializationError
- Since:
- 4.13
-
-
Method Detail
-
createTestClass
@Deprecated protected TestClass createTestClass(java.lang.Class<?> testClass)
Deprecated.Please useParentRunner(org.junit.runners.model.TestClass)
.- Since:
- 4.12
-
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.
-
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
).
-
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
-
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 after executingstatement
; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into aMultipleFailureException
.
-
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)
-
withInterruptIsolation
protected final Statement withInterruptIsolation(Statement statement)
- Returns:
- a
Statement
: clears interrupt status of current thread after execution of statement
-
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.
-
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
-
order
public void order(Orderer orderer) throws InvalidOrderingException
Implementation ofOrderable.order(Orderer)
.- Specified by:
order
in interfaceOrderable
- Throws:
InvalidOrderingException
- if orderer does something invalid (like remove or add children)- Since:
- 4.13
-
setScheduler
public void setScheduler(RunnerScheduler scheduler)
Sets a scheduler that determines the order and parallelization of children. Highly experimental feature that may change.
-
-