类 ParentRunner<T>
- java.lang.Object
- org.junit.runner.Runner
- org.junit.runners.ParentRunner<T>
- 所有已实现的接口:
Describable
,Filterable
,Sortable
- 直接已知子类:
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.- 从以下版本开始:
- 4.5
构造器概要
构造器 限定符 构造器 说明 protected
ParentRunner(Class<?> testClass)
Constructs a newParentRunner
that will run@TestClass
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 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 List<TestRule>
classRules()
protected void
collectInitializationErrors(List<Throwable> errors)
Adds toerrors
a throwable for each problem noted with the test class (available fromgetTestClass()
).protected TestClass
createTestClass(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 List<T>
getChildren()
Returns a list of objects that define the children of this Runner.Description
getDescription()
protected String
getName()
Returns a name used to describe this Runnerprotected 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()
.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(Class<? extends Annotation> annotation, boolean isStatic, List<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.
构造器详细资料
ParentRunner
protected ParentRunner(Class<?> testClass) throws InitializationError
Constructs a newParentRunner
that will run@TestClass
方法详细资料
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 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(List<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(Class<? extends Annotation> annotation, boolean isStatic, List<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
- 返回:
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 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()
- 返回:
- 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.
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 Annotation[] getRunnerAnnotations()
- 返回:
- the annotations that should be attached to this runner's description.
getDescription
public Description getDescription()
- 指定者:
getDescription
在接口中Describable
- 指定者:
getDescription
在类中Runner
- 返回:
- a
Description
showing the tests to be run by the receiver
run
public void run(RunNotifier notifier)
从类复制的说明:Runner
Run the tests for this runner.
filter
public void filter(Filter filter) throws NoTestsRemainException
从接口复制的说明:Filterable
Remove tests that don't pass the parameterfilter
.- 指定者:
filter
在接口中Filterable
- 参数:
filter
- theFilter
to apply- 抛出:
NoTestsRemainException
- if all tests are filtered out
setScheduler
public void setScheduler(RunnerScheduler scheduler)
Sets a scheduler that determines the order and parallelization of children. Highly experimental feature that may change.