Package org.junit.runners
Class BlockJUnit4ClassRunner
- All Implemented Interfaces:
Describable
,Filterable
,Orderable
,Sortable
- Direct Known Subclasses:
BlockJUnit4ClassRunnerWithParameters
,JUnit4
,Theories
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.
- Since:
- 4.5
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal<RuleContainer>
private final ConcurrentMap<FrameworkMethod,
Description> private static TestClassValidator
-
Constructor Summary
ConstructorsModifierConstructorDescriptionBlockJUnit4ClassRunner
(Class<?> testClass) Creates a BlockJUnit4ClassRunner to runtestClass
protected
BlockJUnit4ClassRunner
(TestClass testClass) Creates a BlockJUnit4ClassRunner to runtestClass
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
collectInitializationErrors
(List<Throwable> errors) Adds toerrors
a throwable for each problem noted with the test class (available fromParentRunner.getTestClass()
).protected List<FrameworkMethod>
Returns the methods that run tests.protected Object
Returns a new fixture for running a test.protected Object
createTest
(FrameworkMethod method) Returns a new fixture to run a particular testmethod
against.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>
Returns a list of objects that define the children of this Runner.getExpectedException
(Test annotation) getTestRules
(Object target) private long
getTimeout
(Test annotation) private boolean
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 theTest.expected()
attribute, return normally only ifnext
throws an exception of the correct type, and throw an exception otherwise.protected List<MethodRule>
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) Deprecated.private void
validateMethods
(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)private void
validatePublicConstructor
(List<Throwable> errors) 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) Deprecated.private Statement
withRules
(FrameworkMethod method, Object target, Statement statement) Methods inherited from class org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, order, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses, withInterruptIsolation
-
Field Details
-
PUBLIC_CLASS_VALIDATOR
-
methodDescriptions
-
CURRENT_RULE_CONTAINER
-
-
Constructor Details
-
BlockJUnit4ClassRunner
Creates a BlockJUnit4ClassRunner to runtestClass
- Throws:
InitializationError
- if the test class is malformed.
-
BlockJUnit4ClassRunner
Creates a BlockJUnit4ClassRunner to runtestClass
.- Throws:
InitializationError
- if the test class is malformed.- Since:
- 4.13
-
-
Method Details
-
runChild
Description copied from class: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
- Specified by:
runChild
in classParentRunner<FrameworkMethod>
-
isIgnored
Evaluates whetherFrameworkMethod
s are ignored based on theIgnore
annotation.- Overrides:
isIgnored
in classParentRunner<FrameworkMethod>
-
describeChild
Description copied from class:ParentRunner
Returns aDescription
forchild
, which can be assumed to be an element of the list returned byParentRunner.getChildren()
- Specified by:
describeChild
in classParentRunner<FrameworkMethod>
-
getChildren
Description copied from class:ParentRunner
Returns a list of objects that define the children of this Runner.- Specified by:
getChildren
in classParentRunner<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
Description copied from class: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.- Overrides:
collectInitializationErrors
in classParentRunner<FrameworkMethod>
-
validatePublicConstructor
-
validateNoNonStaticInnerClass
-
validateConstructor
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
Adds toerrors
if the test class has more than one constructor (do not override) -
validateZeroArgConstructor
Adds toerrors
if the test class's single constructor takes parameters (do not override) -
hasOneConstructor
private boolean hasOneConstructor() -
validateInstanceMethods
Deprecated.Adds toerrors
for each method annotated with@Test
,@Before
, or@After
that is not a public, void instance method with no arguments. -
validateFields
-
validateMethods
-
validateTestMethods
Adds toerrors
for each method annotated with@Test
that is not a public, void instance method with no arguments. -
createTest
Returns a new fixture for running a test. Default implementation executes the test class's no-argument constructor (validation should have ensured one exists).- Throws:
Exception
-
createTest
Returns a new fixture to run a particular testmethod
against. Default implementation executes the no-argumentcreateTest()
method.- Throws:
Exception
- Since:
- 4.13
-
testName
Returns the name that describesmethod
forDescription
s. Default implementation is the method's name -
methodBlock
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(org.junit.runners.model.FrameworkMethod)
, and throw any exceptions thrown by either operation. - HOWEVER, if
method
's@Test
annotation has theTest.expected()
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
-
possiblyExpectingExceptions
protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) Returns aStatement
: ifmethod
's@Test
annotation has theTest.expected()
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) Deprecated.Returns aStatement
: ifmethod
's@Test
annotation has thetimeout
attribute, throw an exception ifnext
takes more than the specified number of milliseconds. -
withBefores
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
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
. -
withRules
-
rules
- Parameters:
target
- the test case instance- Returns:
- a list of MethodRules that should be applied when executing this test
-
getTestRules
- Parameters:
target
- the test case instance- Returns:
- a list of TestRules that should be applied when executing this test
-
getExpectedException
-
getTimeout
-