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,TisMethod. ForSuite,TisClass.) Subclasses must implement finding the children of the node, describing each child, and running each child. ParentRunner will filter and sort children, handle@BeforeClassand@AfterClassmethods, handle annotatedClassRules, create a compositeDescription, and run children sequentially.- Since:
- 4.5
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedParentRunner(java.lang.Class<?> testClass)Constructs a newParentRunnerthat will run@TestClassprotectedParentRunner(TestClass testClass)Constructs a newParentRunnerthat will run theTestClass.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected StatementchildrenInvoker(RunNotifier notifier)Returns aStatement: CallrunChild(Object, RunNotifier)on each object returned bygetChildren()(subject to any imposed filter and sort)protected StatementclassBlock(RunNotifier notifier)Constructs aStatementto run all of the tests in the test class.protected java.util.List<TestRule>classRules()protected voidcollectInitializationErrors(java.util.List<java.lang.Throwable> errors)Adds toerrorsa throwable for each problem noted with the test class (available fromgetTestClass()).protected TestClasscreateTestClass(java.lang.Class<?> testClass)Deprecated.Please useParentRunner(org.junit.runners.model.TestClass).protected abstract DescriptiondescribeChild(T child)Returns aDescriptionforchild, which can be assumed to be an element of the list returned bygetChildren()voidfilter(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.DescriptiongetDescription()protected java.lang.StringgetName()Returns a name used to describe this Runnerprotected java.lang.annotation.Annotation[]getRunnerAnnotations()TestClassgetTestClass()Returns aTestClassobject wrapping the class to be executed.protected booleanisIgnored(T child)Evaluates whether a child is ignored.voidorder(Orderer orderer)Implementation ofOrderable.order(Orderer).voidrun(RunNotifier notifier)Run the tests for this runner.protected abstract voidrunChild(T child, RunNotifier notifier)Runs the test corresponding tochild, which can be assumed to be an element of the list returned bygetChildren().protected voidrunLeaf(Statement statement, Description description, RunNotifier notifier)Runs aStatementthat represents a leaf (aka atomic) test.voidsetScheduler(RunnerScheduler scheduler)Sets a scheduler that determines the order and parallelization of children.voidsort(Sorter sorter)Sorts the tests usingsorterprotected voidvalidatePublicVoidNoArgMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, boolean isStatic, java.util.List<java.lang.Throwable> errors)Adds toerrorsif 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 StatementwithAfterClasses(Statement statement)Returns aStatement: run all non-overridden@AfterClassmethods 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 StatementwithBeforeClasses(Statement statement)Returns aStatement: run all non-overridden@BeforeClassmethods on this class and superclasses before executingstatement; if any throws an Exception, stop execution and pass the exception on.protected StatementwithInterruptIsolation(Statement statement)
-
-
-
Constructor Detail
-
ParentRunner
protected ParentRunner(java.lang.Class<?> testClass) throws InitializationErrorConstructs a newParentRunnerthat will run@TestClass- Throws:
InitializationError
-
ParentRunner
protected ParentRunner(TestClass testClass) throws InitializationError
Constructs a newParentRunnerthat 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 aDescriptionforchild, 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 toerrorsa throwable for each problem noted with the test class (available fromgetTestClass()). Default implementation adds an error for each method annotated with@BeforeClassor@AfterClassthat is notpublic static voidwith 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 toerrorsif 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 aStatementto 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
ClassRules on the test-class and superclasses. - Run all non-overridden
@BeforeClassmethods 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
@AfterClassmethods 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@BeforeClassmethods 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@AfterClassmethods 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
ClassRules 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 theIgnoreannotation.
-
getName
protected java.lang.String getName()
Returns a name used to describe this Runner
-
getTestClass
public final TestClass getTestClass()
Returns aTestClassobject wrapping the class to be executed.
-
runLeaf
protected final void runLeaf(Statement statement, Description description, RunNotifier notifier)
Runs aStatementthat 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:
getDescriptionin interfaceDescribable- Specified by:
getDescriptionin classRunner- Returns:
- a
Descriptionshowing the tests to be run by the receiver
-
run
public void run(RunNotifier notifier)
Description copied from class:RunnerRun the tests for this runner.
-
filter
public void filter(Filter filter) throws NoTestsRemainException
Description copied from interface:FilterableRemove tests that don't pass the parameterfilter.- Specified by:
filterin interfaceFilterable- Parameters:
filter- theFilterto apply- Throws:
NoTestsRemainException- if all tests are filtered out
-
sort
public void sort(Sorter sorter)
Description copied from interface:SortableSorts the tests usingsorter
-
order
public void order(Orderer orderer) throws InvalidOrderingException
Implementation ofOrderable.order(Orderer).- Specified by:
orderin 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.
-
-