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
Statements, allowing new operations to be inserted into the appropriate point in the execution flow. - It is published, and extension and reuse are encouraged, whereas
JUnit4ClassRunnerwas in an internal package, and is now deprecated.
In turn, in 2009 we introduced Rules. In many cases where extending
BlockJUnit4ClassRunner was necessary to add new behavior, Rules 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 runtestClassprotectedBlockJUnit4ClassRunner(TestClass testClass) Creates a BlockJUnit4ClassRunner to runtestClass. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcollectInitializationErrors(List<Throwable> errors) Adds toerrorsa throwable for each problem noted with the test class (available fromParentRunner.getTestClass()).protected List<FrameworkMethod> Returns the methods that run tests.protected ObjectReturns a new fixture for running a test.protected ObjectcreateTest(FrameworkMethod method) Returns a new fixture to run a particular testmethodagainst.protected DescriptiondescribeChild(FrameworkMethod method) Returns aDescriptionforchild, 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 longgetTimeout(Test annotation) private booleanprotected booleanisIgnored(FrameworkMethod child) Evaluates whetherFrameworkMethods are ignored based on theIgnoreannotation.protected StatementmethodBlock(FrameworkMethod method) Returns a Statement that, when executed, either returns normally ifmethodpasses, or throws an exception ifmethodfails.protected StatementmethodInvoker(FrameworkMethod method, Object test) protected StatementpossiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) Returns aStatement: ifmethod's@Testannotation has theTest.expected()attribute, return normally only ifnextthrows an exception of the correct type, and throw an exception otherwise.protected List<MethodRule> protected voidrunChild(FrameworkMethod method, RunNotifier notifier) Runs the test corresponding tochild, which can be assumed to be an element of the list returned byParentRunner.getChildren().protected StringtestName(FrameworkMethod method) Returns the name that describesmethodforDescriptions.protected voidvalidateConstructor(List<Throwable> errors) Adds toerrorsif the test class has more than one constructor, or if the constructor takes parameters.protected voidvalidateFields(List<Throwable> errors) protected voidvalidateInstanceMethods(List<Throwable> errors) Deprecated.private voidvalidateMethods(List<Throwable> errors) protected voidvalidateNoNonStaticInnerClass(List<Throwable> errors) protected voidvalidateOnlyOneConstructor(List<Throwable> errors) Adds toerrorsif the test class has more than one constructor (do not override)private voidvalidatePublicConstructor(List<Throwable> errors) protected voidvalidateTestMethods(List<Throwable> errors) Adds toerrorsfor each method annotated with@Testthat is not a public, void instance method with no arguments.protected voidvalidateZeroArgConstructor(List<Throwable> errors) Adds toerrorsif the test class's single constructor takes parameters (do not override)protected StatementwithAfters(FrameworkMethod method, Object target, Statement statement) Returns aStatement: run all non-overridden@Aftermethods 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 StatementwithBefores(FrameworkMethod method, Object target, Statement statement) Returns aStatement: run all non-overridden@Beforemethods on this class and superclasses before runningnext; if any throws an Exception, stop execution and pass the exception on.protected StatementwithPotentialTimeout(FrameworkMethod method, Object test, Statement next) Deprecated.private StatementwithRules(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:ParentRunnerRuns 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:
runChildin classParentRunner<FrameworkMethod>
-
isIgnored
Evaluates whetherFrameworkMethods are ignored based on theIgnoreannotation.- Overrides:
isIgnoredin classParentRunner<FrameworkMethod>
-
describeChild
Description copied from class:ParentRunnerReturns aDescriptionforchild, which can be assumed to be an element of the list returned byParentRunner.getChildren()- Specified by:
describeChildin classParentRunner<FrameworkMethod>
-
getChildren
Description copied from class:ParentRunnerReturns a list of objects that define the children of this Runner.- Specified by:
getChildrenin classParentRunner<FrameworkMethod>
-
computeTestMethods
Returns the methods that run tests. Default implementation returns all methods annotated with@Teston this class and superclasses that are not overridden. -
collectInitializationErrors
Description copied from class:ParentRunnerAdds toerrorsa throwable for each problem noted with the test class (available fromParentRunner.getTestClass()). Default implementation adds an error for each method annotated with@BeforeClassor@AfterClassthat is notpublic static voidwith no arguments.- Overrides:
collectInitializationErrorsin classParentRunner<FrameworkMethod>
-
validatePublicConstructor
-
validateNoNonStaticInnerClass
-
validateConstructor
Adds toerrorsif the test class has more than one constructor, or if the constructor takes parameters. Override if a subclass requires different validation rules. -
validateOnlyOneConstructor
Adds toerrorsif the test class has more than one constructor (do not override) -
validateZeroArgConstructor
Adds toerrorsif the test class's single constructor takes parameters (do not override) -
hasOneConstructor
private boolean hasOneConstructor() -
validateInstanceMethods
Deprecated.Adds toerrorsfor each method annotated with@Test,@Before, or@Afterthat is not a public, void instance method with no arguments. -
validateFields
-
validateMethods
-
validateTestMethods
Adds toerrorsfor each method annotated with@Testthat 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 testmethodagainst. Default implementation executes the no-argumentcreateTest()method.- Throws:
Exception- Since:
- 4.13
-
testName
Returns the name that describesmethodforDescriptions. Default implementation is the method's name -
methodBlock
Returns a Statement that, when executed, either returns normally ifmethodpasses, or throws an exception ifmethodfails. Here is an outline of the default implementation:- Invoke
methodon the result ofcreateTest(org.junit.runners.model.FrameworkMethod), and throw any exceptions thrown by either operation. - HOWEVER, if
method's@Testannotation 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@Testannotation has thetimeoutattribute, throw an exception if the previous step takes more than the specified number of milliseconds. - ALWAYS run all non-overridden
@Beforemethods 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
@Aftermethods 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
@Rulefields to modify the execution of the above steps. ARulemay 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@Testannotation has theTest.expected()attribute, return normally only ifnextthrows 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@Testannotation has thetimeoutattribute, throw an exception ifnexttakes more than the specified number of milliseconds. -
withBefores
Returns aStatement: run all non-overridden@Beforemethods 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@Aftermethods 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
-