类 BlockJUnit4ClassRunner
- java.lang.Object
- org.junit.runner.Runner
- org.junit.runners.ParentRunner<FrameworkMethod>
- org.junit.runners.BlockJUnit4ClassRunner
- 所有已实现的接口:
Describable
,Filterable
,Sortable
public class BlockJUnit4ClassRunner extends ParentRunner<FrameworkMethod>
Implements the JUnit 4 standard test case class model, as defined by the annotations in the org.junit package. Many users will never notice this class: it is now the default test class runner, but it should have exactly the same behavior as the old test class runner (JUnit4ClassRunner
).BlockJUnit4ClassRunner has advantages for writers of custom JUnit runners that are slight changes to the default behavior, however:
- It has a much simpler implementation based on
Statement
s, allowing new operations to be inserted into the appropriate point in the execution flow. - It is published, and extension and reuse are encouraged, whereas
JUnit4ClassRunner
was in an internal package, and is now deprecated.
In turn, in 2009 we introduced
Rule
s. In many cases where extending BlockJUnit4ClassRunner was necessary to add new behavior,Rule
s can be used, which makes the extension more reusable and composable.- 从以下版本开始:
- 4.5
构造器概要
构造器 构造器 说明 BlockJUnit4ClassRunner(Class<?> klass)
Creates a BlockJUnit4ClassRunner to runklass
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected void
collectInitializationErrors(List<Throwable> errors)
Adds toerrors
a throwable for each problem noted with the test class (available fromParentRunner.getTestClass()
).protected List<FrameworkMethod>
computeTestMethods()
Returns the methods that run tests.protected Object
createTest()
Returns a new fixture for running a test.protected Description
describeChild(FrameworkMethod method)
Returns aDescription
forchild
, which can be assumed to be an element of the list returned byParentRunner.getChildren()
protected List<FrameworkMethod>
getChildren()
Returns a list of objects that define the children of this Runner.protected List<TestRule>
getTestRules(Object target)
protected boolean
isIgnored(FrameworkMethod child)
Evaluates whetherFrameworkMethod
s are ignored based on theIgnore
annotation.protected Statement
methodBlock(FrameworkMethod method)
Returns a Statement that, when executed, either returns normally ifmethod
passes, or throws an exception ifmethod
fails.protected Statement
methodInvoker(FrameworkMethod method, Object test)
protected Statement
possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next)
Returns aStatement
: ifmethod
's@Test
annotation has theexpecting
attribute, return normally only ifnext
throws an exception of the correct type, and throw an exception otherwise.protected List<MethodRule>
rules(Object target)
protected void
runChild(FrameworkMethod method, RunNotifier notifier)
Runs the test corresponding tochild
, which can be assumed to be an element of the list returned byParentRunner.getChildren()
.protected String
testName(FrameworkMethod method)
Returns the name that describesmethod
forDescription
s.protected void
validateConstructor(List<Throwable> errors)
Adds toerrors
if the test class has more than one constructor, or if the constructor takes parameters.protected void
validateFields(List<Throwable> errors)
protected void
validateInstanceMethods(List<Throwable> errors)
已过时。protected void
validateNoNonStaticInnerClass(List<Throwable> errors)
protected void
validateOnlyOneConstructor(List<Throwable> errors)
Adds toerrors
if the test class has more than one constructor (do not override)protected void
validateTestMethods(List<Throwable> errors)
Adds toerrors
for each method annotated with@Test
that is not a public, void instance method with no arguments.protected void
validateZeroArgConstructor(List<Throwable> errors)
Adds toerrors
if the test class's single constructor takes parameters (do not override)protected Statement
withAfters(FrameworkMethod method, Object target, Statement statement)
Returns aStatement
: run all non-overridden@After
methods on this class and superclasses before runningnext
; all After methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from After methods into aMultipleFailureException
.protected Statement
withBefores(FrameworkMethod method, Object target, Statement statement)
Returns aStatement
: run all non-overridden@Before
methods on this class and superclasses before runningnext
; if any throws an Exception, stop execution and pass the exception on.protected Statement
withPotentialTimeout(FrameworkMethod method, Object test, Statement next)
已过时。从类继承的方法 org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses
构造器详细资料
BlockJUnit4ClassRunner
public BlockJUnit4ClassRunner(Class<?> klass) throws InitializationError
Creates a BlockJUnit4ClassRunner to runklass
- 抛出:
InitializationError
- if the test class is malformed.
方法详细资料
runChild
protected void runChild(FrameworkMethod method, RunNotifier notifier)
从类复制的说明:ParentRunner
Runs the test corresponding tochild
, which can be assumed to be an element of the list returned byParentRunner.getChildren()
. Subclasses are responsible for making sure that relevant test events are reported throughnotifier
- 指定者:
runChild
在类中ParentRunner<FrameworkMethod>
isIgnored
protected boolean isIgnored(FrameworkMethod child)
Evaluates whetherFrameworkMethod
s are ignored based on theIgnore
annotation.- 覆盖:
isIgnored
在类中ParentRunner<FrameworkMethod>
describeChild
protected Description describeChild(FrameworkMethod method)
从类复制的说明:ParentRunner
Returns aDescription
forchild
, which can be assumed to be an element of the list returned byParentRunner.getChildren()
- 指定者:
describeChild
在类中ParentRunner<FrameworkMethod>
getChildren
protected List<FrameworkMethod> getChildren()
从类复制的说明:ParentRunner
Returns a list of objects that define the children of this Runner.- 指定者:
getChildren
在类中ParentRunner<FrameworkMethod>
computeTestMethods
protected List<FrameworkMethod> computeTestMethods()
Returns the methods that run tests. Default implementation returns all methods annotated with@Test
on this class and superclasses that are not overridden.
collectInitializationErrors
protected void collectInitializationErrors(List<Throwable> errors)
从类复制的说明:ParentRunner
Adds toerrors
a throwable for each problem noted with the test class (available fromParentRunner.getTestClass()
). Default implementation adds an error for each method annotated with@BeforeClass
or@AfterClass
that is notpublic static void
with no arguments.
validateNoNonStaticInnerClass
protected void validateNoNonStaticInnerClass(List<Throwable> errors)
validateConstructor
protected void validateConstructor(List<Throwable> errors)
Adds toerrors
if the test class has more than one constructor, or if the constructor takes parameters. Override if a subclass requires different validation rules.
validateOnlyOneConstructor
protected void validateOnlyOneConstructor(List<Throwable> errors)
Adds toerrors
if the test class has more than one constructor (do not override)
validateZeroArgConstructor
protected void validateZeroArgConstructor(List<Throwable> errors)
Adds toerrors
if the test class's single constructor takes parameters (do not override)
validateInstanceMethods
@Deprecated protected void validateInstanceMethods(List<Throwable> errors)
已过时。Adds toerrors
for each method annotated with@Test
,@Before
, or@After
that is not a public, void instance method with no arguments.
validateFields
protected void validateFields(List<Throwable> errors)
validateTestMethods
protected void validateTestMethods(List<Throwable> errors)
Adds toerrors
for each method annotated with@Test
that is not a public, void instance method with no arguments.
createTest
protected Object createTest() throws Exception
Returns a new fixture for running a test. Default implementation executes the test class's no-argument constructor (validation should have ensured one exists).- 抛出:
Exception
testName
protected String testName(FrameworkMethod method)
Returns the name that describesmethod
forDescription
s. Default implementation is the method's name
methodBlock
protected Statement methodBlock(FrameworkMethod method)
Returns a Statement that, when executed, either returns normally ifmethod
passes, or throws an exception ifmethod
fails. Here is an outline of the default implementation:- Invoke
method
on the result ofcreateTest()
, and throw any exceptions thrown by either operation. - HOWEVER, if
method
's@Test
annotation has theexpecting
attribute, return normally only if the previous step threw an exception of the correct type, and throw an exception otherwise. - HOWEVER, if
method
's@Test
annotation has thetimeout
attribute, throw an exception if the previous step takes more than the specified number of milliseconds. - ALWAYS run all non-overridden
@Before
methods on this class and superclasses before any of the previous steps; if any throws an Exception, stop execution and pass the exception on. - ALWAYS run all non-overridden
@After
methods on this class and superclasses after any of the previous steps; all After methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from After methods into aMultipleFailureException
. - ALWAYS allow
@Rule
fields to modify the execution of the above steps. ARule
may prevent all execution of the above steps, or add additional behavior before and after, or modify thrown exceptions. For more information, seeTestRule
- Invoke
methodInvoker
protected Statement methodInvoker(FrameworkMethod method, Object test)
possiblyExpectingExceptions
protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next)
Returns aStatement
: ifmethod
's@Test
annotation has theexpecting
attribute, return normally only ifnext
throws an exception of the correct type, and throw an exception otherwise.
withPotentialTimeout
@Deprecated protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next)
已过时。Returns aStatement
: ifmethod
's@Test
annotation has thetimeout
attribute, throw an exception ifnext
takes more than the specified number of milliseconds.
withBefores
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement)
Returns aStatement
: run all non-overridden@Before
methods on this class and superclasses before runningnext
; if any throws an Exception, stop execution and pass the exception on.
withAfters
protected Statement withAfters(FrameworkMethod method, Object target, Statement statement)
Returns aStatement
: run all non-overridden@After
methods on this class and superclasses before runningnext
; all After methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from After methods into aMultipleFailureException
.
rules
protected List<MethodRule> rules(Object target)
- 参数:
target
- the test case instance- 返回:
- a list of MethodRules that should be applied when executing this test
getTestRules
protected List<TestRule> getTestRules(Object target)
- 参数:
target
- the test case instance- 返回:
- a list of TestRules that should be applied when executing this test