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,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(Class<?> testClass)Constructs a newParentRunnerthat will run@TestClass
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete 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 List<TestRule>classRules()protected voidcollectInitializationErrors(List<Throwable> errors)Adds toerrorsa throwable for each problem noted with the test class (available fromgetTestClass()).protected TestClasscreateTestClass(Class<?> 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 List<T>getChildren()Returns a list of objects that define the children of this Runner.DescriptiongetDescription()protected StringgetName()Returns a name used to describe this Runnerprotected Annotation[]getRunnerAnnotations()TestClassgetTestClass()Returns aTestClassobject wrapping the class to be executed.protected booleanisIgnored(T child)Evaluates whether a child is ignored.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(Class<? extends Annotation> annotation, boolean isStatic, List<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 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 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.
-
-
-
Constructor Detail
-
ParentRunner
protected ParentRunner(Class<?> testClass) throws InitializationError
Constructs a newParentRunnerthat will run@TestClass- Throws:
InitializationError
-
-
Method Detail
-
createTestClass
protected TestClass createTestClass(Class<?> testClass)
-
getChildren
protected abstract 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(List<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(Class<? extends Annotation> annotation, boolean isStatic, List<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 before 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 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)
-
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.
-
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 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
-
setScheduler
public void setScheduler(RunnerScheduler scheduler)
Sets a scheduler that determines the order and parallelization of children. Highly experimental feature that may change.
-
-