Uploaded image for project: 'MINA SSHD'
  1. MINA SSHD
  2. SSHD-764

Override default jUnit tests runner so as not to create a new instance for each executed test method

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.7.0
    • 1.7.0
    • Important

    Description

      The default tests runner (if not specified) is BlockJUnit4ClassRunner its default behavior is to create a new instance of the test class for each executed test method. This can be changed by deriving an ExtendedBlockJUnit4ClassRunner and overriding its createTest method:

      public class ExtendedBlockJUnit4ClassRunner {
          private final AtomicReference<Pair<Class<?>, Object>> testHolder = new AtomicReference<>();
      
      protected Object createTest() throws Exception {
          Pair<Class<?>, Object> lastTest = testHolder.get();
          Class<?> lastTestClass = (lastTest == null) ? null : lastTest.getLeft();
          TestClass curTest = getTestClass():
          Class<?> curTestClass = curTest.getJavaClass():
          if (curTestClass == lastTestClass) {
              return lastTest.getRight();
          }
      
          Object instance = curTest.getOnlyConstructor().newInstance();
          testHolder.set(Pair.of(curTestClass, instance));
          return instance;
      }
      

      Attachments

        Activity

          People

            lgoldstein Lyor Goldstein
            lgoldstein Lyor Goldstein
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified