michal_lipski
michal_lipski

Reputation: 25

How to pass argument to extension class constructor in JUnit5

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?

Upvotes: 2

Views: 2116

Answers (1)

johanneslink
johanneslink

Reputation: 5351

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.

Upvotes: 1

Related Questions