简体   繁体   中英

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.

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 (eg, 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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM