public abstract class ParentRunner<T> extends Runner implements Filterable, Orderable
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.Modifier and Type | Class and Description |
---|---|
private static class |
ParentRunner.ClassRuleCollector |
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.locks.Lock |
childrenLock |
private java.util.List<T> |
filteredChildren |
private RunnerScheduler |
scheduler |
private TestClass |
testClass |
private static java.util.List<TestClassValidator> |
VALIDATORS |
Modifier | Constructor and Description |
---|---|
protected |
ParentRunner(java.lang.Class<?> testClass)
Constructs a new
ParentRunner that will run @TestClass |
protected |
ParentRunner(TestClass testClass)
Constructs a new
ParentRunner that will run the TestClass . |
Modifier and Type | Method and Description |
---|---|
private void |
applyValidators(java.util.List<java.lang.Throwable> errors) |
private boolean |
areAllChildrenIgnored() |
protected Statement |
childrenInvoker(RunNotifier notifier)
Returns a
Statement : Call runChild(Object, RunNotifier)
on each object returned by getChildren() (subject to any imposed
filter and sort) |
protected Statement |
classBlock(RunNotifier notifier)
Constructs a
Statement 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 to
errors a throwable for each problem noted with the test class (available from getTestClass() ). |
private java.util.Comparator<? super T> |
comparator(Sorter sorter) |
protected TestClass |
createTestClass(java.lang.Class<?> testClass)
Deprecated.
Please use
ParentRunner(org.junit.runners.model.TestClass) . |
protected abstract Description |
describeChild(T child)
Returns a
Description for child , which can be assumed to
be an element of the list returned by getChildren() |
void |
filter(Filter filter)
Remove tests that don't pass the parameter
filter . |
protected abstract java.util.List<T> |
getChildren()
Returns a list of objects that define the children of this Runner.
|
Description |
getDescription() |
private java.util.List<T> |
getFilteredChildren() |
protected java.lang.String |
getName()
Returns a name used to describe this Runner
|
protected java.lang.annotation.Annotation[] |
getRunnerAnnotations() |
TestClass |
getTestClass()
Returns a
TestClass object wrapping the class to be executed. |
protected boolean |
isIgnored(T child)
Evaluates whether a child is ignored.
|
void |
order(Orderer orderer)
Implementation of
Orderable.order(Orderer) . |
void |
run(RunNotifier notifier)
Run the tests for this runner.
|
protected abstract void |
runChild(T child,
RunNotifier notifier)
Runs the test corresponding to
child , which can be assumed to be
an element of the list returned by getChildren() . |
private void |
runChildren(RunNotifier notifier) |
protected void |
runLeaf(Statement statement,
Description description,
RunNotifier notifier)
Runs a
Statement that represents a leaf (aka atomic) test. |
void |
setScheduler(RunnerScheduler scheduler)
Sets a scheduler that determines the order and parallelization
of children.
|
private boolean |
shouldNotReorder() |
private boolean |
shouldRun(Filter filter,
T each) |
void |
sort(Sorter sorter)
Sorts the tests using
sorter |
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 to
errors if any method in this class is annotated with
annotation , 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 ). |
protected Statement |
withAfterClasses(Statement statement)
Returns a
Statement : run all non-overridden @AfterClass methods on this class
and superclasses after executing statement ; all AfterClass methods are
always executed: exceptions thrown by previous steps are combined, if
necessary, with exceptions from AfterClass methods into a
MultipleFailureException . |
protected Statement |
withBeforeClasses(Statement statement)
Returns a
Statement : run all non-overridden @BeforeClass methods on this class
and superclasses before executing statement ; if any throws an
Exception, stop execution and pass the exception on. |
private Statement |
withClassRules(Statement statement)
|
protected Statement |
withInterruptIsolation(Statement statement) |
private static final java.util.List<TestClassValidator> VALIDATORS
private final java.util.concurrent.locks.Lock childrenLock
private final TestClass testClass
private volatile java.util.List<T> filteredChildren
private volatile RunnerScheduler scheduler
protected ParentRunner(java.lang.Class<?> testClass) throws InitializationError
ParentRunner
that will run @TestClass
InitializationError
protected ParentRunner(TestClass testClass) throws InitializationError
ParentRunner
that will run the TestClass
.InitializationError
@Deprecated protected TestClass createTestClass(java.lang.Class<?> testClass)
ParentRunner(org.junit.runners.model.TestClass)
.protected abstract java.util.List<T> getChildren()
protected abstract Description describeChild(T child)
Description
for child
, which can be assumed to
be an element of the list returned by getChildren()
protected abstract void runChild(T child, RunNotifier notifier)
child
, which can be assumed to be
an element of the list returned by getChildren()
.
Subclasses are responsible for making sure that relevant test events are
reported through notifier
protected void collectInitializationErrors(java.util.List<java.lang.Throwable> errors)
errors
a throwable for each problem noted with the test class (available from getTestClass()
).
Default implementation adds an error for each method annotated with
@BeforeClass
or @AfterClass
that is not
public static void
with no arguments.private void applyValidators(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)
errors
if any method in this class is annotated with
annotation
, but:
isStatic is false
), or
isStatic is true
).
private void validateClassRules(java.util.List<java.lang.Throwable> errors)
protected Statement classBlock(RunNotifier notifier)
Statement
to run all of the tests in the test class.
Override to add pre-/post-processing. Here is an outline of the
implementation:
getChildren()
(subject to any imposed filter and sort).ClassRule
s on the test-class and superclasses.@BeforeClass
methods on the test-class
and superclasses; if any throws an Exception, stop execution and pass the
exception on.@AfterClass
methods on the test-class
and superclasses: exceptions thrown by previous steps are combined, if
necessary, with exceptions from AfterClass methods into a
MultipleFailureException
.Statement
private boolean areAllChildrenIgnored()
protected Statement withBeforeClasses(Statement statement)
Statement
: run all non-overridden @BeforeClass
methods on this class
and superclasses before executing statement
; if any throws an
Exception, stop execution and pass the exception on.protected Statement withAfterClasses(Statement statement)
Statement
: run all non-overridden @AfterClass
methods on this class
and superclasses after executing statement
; all AfterClass methods are
always executed: exceptions thrown by previous steps are combined, if
necessary, with exceptions from AfterClass methods into a
MultipleFailureException
.private Statement withClassRules(Statement statement)
statement
- the base statementRule
s are
found, or the base statementprotected java.util.List<TestRule> classRules()
ClassRule
s that can transform the block that runs
each method in the tested class.protected Statement childrenInvoker(RunNotifier notifier)
Statement
: Call runChild(Object, RunNotifier)
on each object returned by getChildren()
(subject to any imposed
filter and sort)protected final Statement withInterruptIsolation(Statement statement)
Statement
: clears interrupt status of current thread after execution of statementprotected boolean isIgnored(T child)
false
.
BlockJUnit4ClassRunner
, for example, overrides this method to
filter tests based on the Ignore
annotation.
private void runChildren(RunNotifier notifier)
protected java.lang.String getName()
public final TestClass getTestClass()
TestClass
object wrapping the class to be executed.protected final void runLeaf(Statement statement, Description description, RunNotifier notifier)
Statement
that represents a leaf (aka atomic) test.protected java.lang.annotation.Annotation[] getRunnerAnnotations()
public Description getDescription()
getDescription
in interface Describable
getDescription
in class Runner
Description
showing the tests to be run by the receiverpublic void run(RunNotifier notifier)
Runner
public void filter(Filter filter) throws NoTestsRemainException
Filterable
filter
.filter
in interface Filterable
filter
- the Filter
to applyNoTestsRemainException
- if all tests are filtered outpublic void sort(Sorter sorter)
Sortable
sorter
public void order(Orderer orderer) throws InvalidOrderingException
Orderable.order(Orderer)
.order
in interface Orderable
InvalidOrderingException
- if orderer does something invalid (like remove or add
children)private boolean shouldNotReorder()
private void validate() throws InitializationError
InitializationError
private java.util.List<T> getFilteredChildren()
public void setScheduler(RunnerScheduler scheduler)