1

I have a problem to pass argument from test class to its extension class constructor. Problem occurred during migration from JUnit4 to JUnit5. (For JUnit4 worked fine) In JUnit4 test class looked like:

public class TestClass{
    @Rule
    @Autowired
    public SupportClass supportClass;
}

and Support class looked:

   @Component
   @RequiredArgsConstructor
   public class SupportClass implements TestRule {
        private final EntityManager entityManager;

        @Override
        public Statement apply(Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                final Statistics statistics = ((Session) entityManager.getDelegate()).getSessionFactory().getStatistics();
                statistics.clear();

                base.evaluate();
            }
         };
        }
   }

In JUnit5 I am trying to do (test class):

@ExtendWith(SupportClass.class)
public class TestClass{

    @Autowired
    public SupportClass supportClass;
}

and Support class looks:

   @Component
   @RequiredArgsConstructor
   public class SupportClass implements AfterEachCallback, BeforeEachCallback {
        private final EntityManager entityManager;

    @Override
    public void beforeEach(ExtensionContext extensionContext) throws Exception {
        final Statistics statistics = ((Session) entityManager.getDelegate()).getSessionFactory().getStatistics();
        statistics.clear();

    }

    @Override
    public void afterEach(ExtensionContext extensionContext) throws Exception {
        final Statistics statistics = ((Session) entityManager.getDelegate()).getSessionFactory().getStatistics();
        statistics.clear();

    }
   }

And when I am trying to execute test I get java.lang.NoSuchMethodException: .SupportClass.init() exception. I know it is related to missing non args constructor. Unfortunately my SupportClass requires this argument to work. The question is how to pass this entityManager from test class to support as argument?

  • Have a look at https://junit.org/junit5/docs/5.1.1/api/org/junit/jupiter/api/extension/RegisterExtension.html – johanneslink Sep 23 '20 at 16:41
  • Tried to put it over supportClass but it did not help. I have read this doc but I can not find any explanation how to exactly do that. – michal_lipski Sep 24 '20 at 06:57
  • I have also tried with this tutorial [Guide JUnit5](https://www.baeldung.com/junit-5-extensions), with parameter resolver interface, but still no effect – michal_lipski Sep 25 '20 at 10:30

1 Answers1

1

Here's a sketch. Not checked for compilation errors, no imports and probably not all details:

@SpringBootTest
public class TestClass{

    @Autowired
    @RegisterExtension
    SupportClass support;

    @Test
    void myTest() {...}
}

public class SupportClass implements AfterEachCallback, BeforeEachCallback {
    private final EntityManager entityManager;

    SupportClass(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public void beforeEach(ExtensionContext extensionContext) throws Exception {
        final Statistics statistics = ((Session) entityManager.getDelegate()).getSessionFactory().getStatistics();
        statistics.clear();
    }

    @Override
    public void afterEach(ExtensionContext extensionContext) throws Exception {
        final Statistics statistics = ((Session) entityManager.getDelegate()).getSessionFactory().getStatistics();
        statistics.clear();

    }
}

I think SupportClass is not a good name, but that's a different story.

johanneslink
  • 4,877
  • 1
  • 20
  • 37