如何在运行时为当前测试ApplicationContext注册Spring Context事件 [英] How to register Spring Context Events for current test ApplicationContext at runtime

查看:128
本文介绍了如何在运行时为当前测试ApplicationContext注册Spring Context事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我的目标是在Spring环境中执行JUnit 5测试时收集各种时间戳,以创建不同任务持续时间的统计信息. (可供参考的存储库)

My goal is to collect various timestamps while executing JUnit 5 tests in a Spring environment to create a statistic of the durations of the different tasks. (Repository for reference)

由于我无法触摸实际的测试代码,因此必须注册这些回调 /InsightConfiguration.kt"rel =" nofollow noreferrer> Spring配置对象,并希望通过测试应用程序中的某些组件扫描找到它.正如您可以想象的那样,这并不是在每个测试上下文中都能可靠地工作.

Because I can't touch the actual test code, I have to register callbacks like the ContextRefreshedEvent from within my JUnit Jupiter extension. Currently I am registering these callbacks in a Spring configuration object and hope that this is found by some component scan in the test application. As you can imagine this does not work reliably for every test context.

问题:

我希望我能以某种方式在

I hope that I can somehow register these events like the ContextRefreshedEvent manually in my first callback method from JUnit. As far as I understand, this requires me to add either a Configuration or a Component containing the callbacks to the currently running Spring Context at runtime.

是否可以解决您的最初问题?非常感谢您对此问题的任何投入,因为我已经花了相当长的时间对此进行研究.

Is that possible or do you know any other solution to my initial problem? I would really appreciate any input you have on this issue because I am working on this for quite a while now.

非常感谢您!

推荐答案

更新:

Spring Framework 5.2将具有对测试事件的内置支持.有关详细信息,请参见 GitHub问题18490 .

Spring Framework 5.2 will have built-in support for test events. See GitHub issue 18490 for details.

在Spring ApplicationContext中触发与测试相关的事件的最好方法是实现自定义(Spring)TestExecutionListener,该自定义事件通过ApplicationEventPublisher API(由ApplicationContext实现)触发事件.

The best way to fire test-related events in a Spring ApplicationContext is to implement a custom (Spring) TestExecutionListener that fires the events via the ApplicationEventPublisher API (which ApplicationContext implements).

您可以通过TestContext#getApplicationContext()在自定义TestExecutionListener的生命周期方法中访问测试的ApplicationContext.

You can access the test's ApplicationContext within lifecycle methods in your custom TestExecutionListener via TestContext#getApplicationContext().

为避免触摸测试代码,您可以让Spring自动注册您的自定义TestExecutionListener,如下所述:

To avoid having to touch test code, you can have Spring automatically register your custom TestExecutionListener, as described here: https://docs.spring.io/spring/docs/5.1.4.RELEASE/spring-framework-reference/testing.html#testcontext-tel-config-automatic-discovery

您的自定义TestExecutionListener然后将自动应用于通过Spring TestContext Framework运行的所有测试-除非将给定的测试类配置为覆盖默认侦听器(例如,通过@TestExecutionListeners(...)).

Your custom TestExecutionListener will then be automatically applied to all tests run via the Spring TestContext Framework -- unless a given test class is configured to override the default listeners (e.g., via @TestExecutionListeners(...)).

有关相关讨论,请参见以下Spring JIRA问题:

For related discussion, see the following Spring JIRA issues:

  • https://jira.spring.io/browse/SPR-8710
  • https://jira.spring.io/browse/SPR-13916

此致

Sam( Spring TestContext Framework 的作者)

这篇关于如何在运行时为当前测试ApplicationContext注册Spring Context事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆