smallufo
smallufo

Reputation: 11806

How to get JUnit5's TestInstance's LifeCycle in Spring's TestExectionListener?

Is there any way to get TestInstance.LifeCycle (PER_CLASS or PER_METHOD) in SpringTest's TestExecutionListener ?

For example , in method afterTestMethod(TestContext testContext) , how to get current TestInstance's LifeCycle ?

I looked into TestContext code but seems no related API.

Then, I tried to traverse testContext.getTextClass().getDeclaredAnnotations() , but I think it is not sufficient, as JUnit5 supports declaring default lifecycle in properties or system environment.

Is there any other way to achieve this ? Thanks.

Upvotes: 0

Views: 310

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31247

No, you cannot access JUnit Jupiter APIs or features such as the test instance lifecycle from a Spring TestExecutionListener.

The Spring TestExecutionListener and TestContext APIs are agnostic of the underlying testing framework (e.g., JUnit, TestNG, etc.).

If you need access to JUnit-specific features, you might consider implementing a JUnit Jupiter Extension instead of a Spring TestExecutionListener. Note that a Spring TestExecutionListener can obtain access the Spring ApplicationContext via SpringExtension.getApplicationContext(ExtensionContext).

Upvotes: 2

Related Questions