Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7731

Provide @Rule alternative to SpringJUnit4ClassRunner

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 4.2 RC1
    • Component/s: Test
    • Last commented by a User:
      false

      Description

      Overview

      Sometimes (always?) it would be nice to have a @Rule implementation that did the job of the SpringJUnit4ClassRunner.

      The main motivation for this is to be able to use other runners (e.g. @Parameterized, @Categories, Mockito, etc.).

      There might even be a case for deprecating the runner in favor of a @Rule?


      Further Resources

      1. SpringContextRule.java
        5 kB
        Neale Upstone
      2. SpringContextRule.java
        4 kB
        Neale Upstone

        Issue Links

          Activity

          Hide
          sbrannen Sam Brannen added a comment -

          Hi everybody,

          I'm going to hold off on pushing my branch to master for a few more days.

          That will give more of you a chance to try out the snapshot (see comments above) and provide feedback.

          Cheers,

          Sam

          Show
          sbrannen Sam Brannen added a comment - Hi everybody, I'm going to hold off on pushing my branch to master for a few more days. That will give more of you a chance to try out the snapshot (see comments above) and provide feedback. Cheers, Sam
          Hide
          sbrannen Sam Brannen added a comment -

          The Alchemist, did you have a chance to try out the new rule support yet?

          Show
          sbrannen Sam Brannen added a comment - The Alchemist , did you have a chance to try out the new rule support yet?
          Hide
          sbrannen Sam Brannen added a comment -

          FYI: I have committed some changes with bug fixes and complete Javadoc. These are included in latest 4.2.0.SPR-7731-SNAPSHOT that was just published on http://repo.spring.io/libs-snapshot-local/.

          Feel free to give it a try!

          Show
          sbrannen Sam Brannen added a comment - FYI: I have committed some changes with bug fixes and complete Javadoc. These are included in latest 4.2.0. SPR-7731 -SNAPSHOT that was just published on http://repo.spring.io/libs-snapshot-local/ . Feel free to give it a try!
          Hide
          sbrannen Sam Brannen added a comment -

          Implemented as described in GitHub commit d1b1c4f:

          Introduce JUnit Rule alternative to SpringJUnit4ClassRunner

          Since Spring Framework 2.5, support for integrating the Spring TestContext Framework (TCF) into JUnit 4 based tests has been provided via the SpringJUnit4ClassRunner, but this approach precludes the ability for tests to be run with alternative runners like JUnit's Parameterized or third-party runners such as the MockitoJUnitRunner.

          This commit remedies this situation by introducing @ClassRule and @Rule based alternatives to the SpringJUnit4ClassRunner. These rules are independent of any Runner and can therefore be combined with alternative runners.

          Due to the limitations of JUnit's implementation of rules, as of JUnit 4.12 it is currently impossible to create a single rule that can be applied both at the class level and at the method level (with access to the test instance). Consequently, this commit introduces the following two rules that must be used together.

          • SpringClassRule: a JUnit TestRule that provides the class-level functionality of the TCF to JUnit-based tests
          • SpringMethodRule: a JUnit MethodRule that provides the instance-level and method-level functionality of the TCF to JUnit-based tests

          In addition, this commit also introduces the following new JUnit Statements for use with rules:

          • RunPrepareTestInstanceCallbacks
          • ProfileValueChecker
          Show
          sbrannen Sam Brannen added a comment - Implemented as described in GitHub commit d1b1c4f : Introduce JUnit Rule alternative to SpringJUnit4ClassRunner Since Spring Framework 2.5, support for integrating the Spring TestContext Framework (TCF) into JUnit 4 based tests has been provided via the SpringJUnit4ClassRunner , but this approach precludes the ability for tests to be run with alternative runners like JUnit's Parameterized or third-party runners such as the MockitoJUnitRunner . This commit remedies this situation by introducing @ClassRule and @Rule based alternatives to the SpringJUnit4ClassRunner . These rules are independent of any Runner and can therefore be combined with alternative runners. Due to the limitations of JUnit's implementation of rules, as of JUnit 4.12 it is currently impossible to create a single rule that can be applied both at the class level and at the method level ( with access to the test instance). Consequently, this commit introduces the following two rules that must be used together. SpringClassRule : a JUnit TestRule that provides the class-level functionality of the TCF to JUnit-based tests SpringMethodRule : a JUnit MethodRule that provides the instance-level and method-level functionality of the TCF to JUnit-based tests In addition, this commit also introduces the following new JUnit Statements for use with rules: RunPrepareTestInstanceCallbacks ProfileValueChecker
          Hide
          sbrannen Sam Brannen added a comment -

          FYI: minor changes in latest commit:

          Introduce TestContextManager cache in SpringClassRule

          In order to simplify configuration of the SpringMethodRule and to ensure
          that the correct TestContextManager is always retrieved for the
          currently executing test class, this commit introduces a static
          TestContextManager cache in SpringClassRule.

          In addition, since it is not foreseen that SpringClassRule and
          SpringMethodRule should be able to be subclassed, their internal methods
          are now private instead of protected.

          Consequently, the required configuration now looks like this:

          @ClassRule
          public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
           
          @Rule
          public final SpringMethodRule springMethodRule = new SpringMethodRule();
          

          The only change from the user's perspective is that this no longer has to be supplied to the SpringMethodRule constructor.

          Regards,

          Sam

          Show
          sbrannen Sam Brannen added a comment - FYI: minor changes in latest commit : Introduce TestContextManager cache in SpringClassRule In order to simplify configuration of the SpringMethodRule and to ensure that the correct TestContextManager is always retrieved for the currently executing test class, this commit introduces a static TestContextManager cache in SpringClassRule. In addition, since it is not foreseen that SpringClassRule and SpringMethodRule should be able to be subclassed, their internal methods are now private instead of protected. Consequently, the required configuration now looks like this: @ClassRule public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();   @Rule public final SpringMethodRule springMethodRule = new SpringMethodRule(); The only change from the user's perspective is that this no longer has to be supplied to the SpringMethodRule constructor. Regards, Sam

            People

            • Assignee:
              sbrannen Sam Brannen
              Reporter:
              david_syer Dave Syer
              Last updater:
              Sam Brannen
            • Votes:
              25 Vote for this issue
              Watchers:
              29 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                6 days ago