简体   繁体   English

如何在Spring的TestExectionListener中获取JUnit5的TestInstance的LifeCycle?

[英]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 ?有没有办法在 SpringTest 的TestExecutionListener中获取TestInstance.LifeCycle (PER_CLASS 或 PER_METHOD)?

For example, in method afterTestMethod(TestContext testContext) , how to get current TestInstance's LifeCycle?比如在方法afterTestMethod(TestContext testContext)中,如何获取当前TestInstance的LifeCycle?

I looked into TestContext code but seems no related API.我查看了 TestContext 代码,但似乎没有相关的 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.然后,我尝试遍历testContext.getTextClass().getDeclaredAnnotations() ,但我认为这还不够,因为 JUnit5 支持在属性或系统环境中声明默认生命周期。

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 .不,您不能从 Spring TestExecutionListener访问 JUnit Jupiter API 或测试实例生命周期等功能。

The Spring TestExecutionListener and TestContext APIs are agnostic of the underlying testing framework (eg, JUnit, TestNG, etc.). Spring TestExecutionListenerTestContext API 与底层测试框架无关(例如,JUnit、TestNG 等)。

If you need access to JUnit-specific features, you might consider implementing a JUnit Jupiter Extension instead of a Spring TestExecutionListener .如果您需要访问特定于 JUnit 的功能,您可以考虑实现 JUnit Jupiter Extension而不是 Spring TestExecutionListener Note that a Spring TestExecutionListener can obtain access the Spring ApplicationContext via SpringExtension.getApplicationContext(ExtensionContext) .请注意,Spring TestExecutionListener可以通过SpringExtension.getApplicationContext(ExtensionContext)获取对 Spring ApplicationContext的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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