Class BlockJUnit4ClassRunner

  • All Implemented Interfaces:
    Describable, Filterable, Sortable
    Direct Known Subclasses:
    BlockJUnit4ClassRunnerWithParameters, JUnit4, Theories

    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 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 JUnit4ClassRunner was 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
    • Constructor Detail

      • BlockJUnit4ClassRunner

        public BlockJUnit4ClassRunner​(java.lang.Class<?> klass)
                               throws InitializationError
        Creates a BlockJUnit4ClassRunner to run klass
        Throws:
        InitializationError - if the test class is malformed.
    • Method Detail

      • computeTestMethods

        protected java.util.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​(java.util.List<java.lang.Throwable> errors)
        Description copied from class: ParentRunner
        Adds to errors a throwable for each problem noted with the test class (available from ParentRunner.getTestClass()). Default implementation adds an error for each method annotated with @BeforeClass or @AfterClass that is not public static void with no arguments.
        Overrides:
        collectInitializationErrors in class ParentRunner<FrameworkMethod>
      • validateNoNonStaticInnerClass

        protected void validateNoNonStaticInnerClass​(java.util.List<java.lang.Throwable> errors)
      • validateConstructor

        protected void validateConstructor​(java.util.List<java.lang.Throwable> errors)
        Adds to errors 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​(java.util.List<java.lang.Throwable> errors)
        Adds to errors if the test class has more than one constructor (do not override)
      • validateZeroArgConstructor

        protected void validateZeroArgConstructor​(java.util.List<java.lang.Throwable> errors)
        Adds to errors if the test class's single constructor takes parameters (do not override)
      • hasOneConstructor

        private boolean hasOneConstructor()
      • validateInstanceMethods

        @Deprecated
        protected void validateInstanceMethods​(java.util.List<java.lang.Throwable> errors)
        Deprecated.
        Adds to errors for each method annotated with @Test, @Before, or @After that is not a public, void instance method with no arguments.
      • validateFields

        protected void validateFields​(java.util.List<java.lang.Throwable> errors)
      • validateMethods

        private void validateMethods​(java.util.List<java.lang.Throwable> errors)
      • validateTestMethods

        protected void validateTestMethods​(java.util.List<java.lang.Throwable> errors)
        Adds to errors for each method annotated with @Testthat is not a public, void instance method with no arguments.
      • createTest

        protected java.lang.Object createTest()
                                       throws java.lang.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).
        Throws:
        java.lang.Exception
      • testName

        protected java.lang.String testName​(FrameworkMethod method)
        Returns the name that describes method for Descriptions. Default implementation is the method's name
      • methodBlock

        protected Statement methodBlock​(FrameworkMethod method)
        Returns a Statement that, when executed, either returns normally if method passes, or throws an exception if method fails. Here is an outline of the default implementation:
        • Invoke method on the result of createTest(), and throw any exceptions thrown by either operation.
        • HOWEVER, if method's @Test annotation has the expecting 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 the timeout 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 a MultipleFailureException.
        • ALWAYS allow @Rule fields to modify the execution of the above steps. A Rule may prevent all execution of the above steps, or add additional behavior before and after, or modify thrown exceptions. For more information, see TestRule
        This can be overridden in subclasses, either by overriding this method, or the implementations creating each sub-statement.
      • possiblyExpectingExceptions

        protected Statement possiblyExpectingExceptions​(FrameworkMethod method,
                                                        java.lang.Object test,
                                                        Statement next)
        Returns a Statement: if method's @Test annotation has the expecting attribute, return normally only if next throws an exception of the correct type, and throw an exception otherwise.
      • withPotentialTimeout

        @Deprecated
        protected Statement withPotentialTimeout​(FrameworkMethod method,
                                                 java.lang.Object test,
                                                 Statement next)
        Deprecated.
        Returns a Statement: if method's @Test annotation has the timeout attribute, throw an exception if next takes more than the specified number of milliseconds.
      • withBefores

        protected Statement withBefores​(FrameworkMethod method,
                                        java.lang.Object target,
                                        Statement statement)
        Returns a Statement: run all non-overridden @Before methods on this class and superclasses before running next; if any throws an Exception, stop execution and pass the exception on.
      • withAfters

        protected Statement withAfters​(FrameworkMethod method,
                                       java.lang.Object target,
                                       Statement statement)
        Returns a Statement: run all non-overridden @After methods on this class and superclasses before running next; all After methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from After methods into a MultipleFailureException.
      • getMethodRules

        private java.util.List<MethodRule> getMethodRules​(java.lang.Object target)
      • rules

        protected java.util.List<MethodRule> rules​(java.lang.Object target)
        Parameters:
        target - the test case instance
        Returns:
        a list of MethodRules that should be applied when executing this test
      • withTestRules

        private Statement withTestRules​(FrameworkMethod method,
                                        java.util.List<TestRule> testRules,
                                        Statement statement)
        Returns a Statement: apply all non-static fields annotated with Rule.
        Parameters:
        statement - The base statement
        Returns:
        a RunRules statement if any class-level Rules are found, or the base statement
      • getTestRules

        protected java.util.List<TestRule> getTestRules​(java.lang.Object target)
        Parameters:
        target - the test case instance
        Returns:
        a list of TestRules that should be applied when executing this test
      • getExpectedException

        private java.lang.Class<? extends java.lang.Throwable> getExpectedException​(Test annotation)
      • expectsException

        private boolean expectsException​(Test annotation)
      • getTimeout

        private long getTimeout​(Test annotation)