Class AnnotatedBuilder


  • public class AnnotatedBuilder
    extends RunnerBuilder
    The AnnotatedBuilder is a strategy for constructing runners for test class that have been annotated with the @RunWith annotation. All tests within this class will be executed using the runner that was specified within the annotation.

    If a runner supports inner member classes, the member classes will inherit the runner from the enclosing class, e.g.:

     @RunWith(MyRunner.class)
     public class MyTest {
         // some tests might go here
    
         public class MyMemberClass {
             @Test
             public void thisTestRunsWith_MyRunner() {
                 // some test logic
             }
    
             // some more tests might go here
         }
    
         @RunWith(AnotherRunner.class)
         public class AnotherMemberClass {
             // some tests might go here
    
             public class DeepInnerClass {
                 @Test
                 public void thisTestRunsWith_AnotherRunner() {
                     // some test logic
                 }
             }
    
             public class DeepInheritedClass extends SuperTest {
                 @Test
                 public void thisTestRunsWith_SuperRunner() {
                     // some test logic
                 }
             }
         }
     }
    
     @RunWith(SuperRunner.class)
     public class SuperTest {
         // some tests might go here
     }
     
    The key points to note here are:
    • If there is no RunWith annotation, no runner will be created.
    • The resolve step is inside-out, e.g. the closest RunWith annotation wins
    • RunWith annotations are inherited and work as if the class was annotated itself.
    • The default JUnit runner does not support inner member classes, so this is only valid for custom runners that support inner member classes.
    • Custom runners with support for inner classes may or may not support RunWith annotations for member classes. Please refer to the custom runner documentation.
    Since:
    4.0
    See Also:
    RunnerBuilder, RunWith
    • Field Detail

      • CONSTRUCTOR_ERROR_FORMAT

        private static final java.lang.String CONSTRUCTOR_ERROR_FORMAT
        See Also:
        Constant Field Values
    • Constructor Detail

      • AnnotatedBuilder

        public AnnotatedBuilder​(RunnerBuilder suiteBuilder)
    • Method Detail

      • runnerForClass

        public Runner runnerForClass​(java.lang.Class<?> testClass)
                              throws java.lang.Exception
        Description copied from class: RunnerBuilder
        Override to calculate the correct runner for a test class at runtime.
        Specified by:
        runnerForClass in class RunnerBuilder
        Parameters:
        testClass - class to be run
        Returns:
        a Runner
        Throws:
        java.lang.Exception
      • getEnclosingClassForNonStaticMemberClass

        private java.lang.Class<?> getEnclosingClassForNonStaticMemberClass​(java.lang.Class<?> currentTestClass)
      • buildRunner

        public Runner buildRunner​(java.lang.Class<? extends Runner> runnerClass,
                                  java.lang.Class<?> testClass)
                           throws java.lang.Exception
        Throws:
        java.lang.Exception