什么是TestExecutionListener,它们是做什么的? [英] What are TestExecutionListeners, and what do they do?

查看:785
本文介绍了什么是TestExecutionListener,它们是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,TestExecutionListeners的行为类似于JUnit中的@BeforeClass方法.我不明白的是为什么我需要使用DependencyInjectionTestExecutionListenerTransactionalTestExecutionListenerDirtiesContextTestExecutionListener来使用DbUnitTestExecutionListener.

As far as I understand, TestExecutionListeners act like @BeforeClass methods in JUnit. What I don't understand is why I need to use DependencyInjectionTestExecutionListener, TransactionalTestExecutionListener and DirtiesContextTestExecutionListener to use DbUnitTestExecutionListener.

通常没有DbUnit,我可以创建并填充数据库.为什么突然需要使用这些侦听器为我的数据库做一些CRUD?

Normally without DbUnit, I can create and populate the database. Why suddenly do I need to use these listeners to do some CRUD for my database?

推荐答案

TestExecutionListeners为Spring TestContext Framework中运行的测试提供各种类型的功能.

TestExecutionListeners provide various types of functionality to tests running in the Spring TestContext Framework.

如果您对特定侦听器的功能感兴趣,最好的查找方法是阅读相应类的Javadoc.此外, 测试一章详细介绍了如何使用每个侦听器以及它们的作用.

If you are interested in what a particular listener does, the best way to find out is to read the Javadoc for the respective class. In addition, the Testing chapter of the Spring reference manual goes into detail about how to use each of the listeners and what they do.

在特定情况下,如果您不使用@DirtiesContext,则无需使用DirtiesContextTestExecutionListener.对于DependencyInjectionTestExecutionListenerTransactionalTestExecutionListener,您可能需要它们将依赖项注入到测试中(例如,通过@Autowired@Inject@Resource等),并用于事务测试(即,带有注释的测试) @Transactional).

In your particular case, if you're not using @DirtiesContext, then you don't need to use the DirtiesContextTestExecutionListener. As for DependencyInjectionTestExecutionListener and TransactionalTestExecutionListener, you will likely need them to inject dependencies into your test (e.g., via @Autowired, @Inject, @Resource, etc.) and for transactional tests (i.e., tests annotated with @Transactional).

还请注意,上述侦听器默认情况下处于启用状态.因此,如果您一直在使用Spring TestContext Framework 而没有任何自定义侦听器(例如DbUnit的侦听器),那么您永远不会意识到侦听器的存在.关于 TestExecutionListener配置的部分参考手册还应该帮助您弄清楚事情.但是请注意,合并和自动检测默认侦听器等某些功能仅在Spring Framework 4.1+中可用.

Note as well that the aforementioned listeners are enabled by default. So if you've been using the Spring TestContext Framework without any custom listeners like the one for DbUnit, then you just never realized that the listeners existed. The section on TestExecutionListener configuration in the reference manual should also help clarify things. Note, however, that some features like merging and auto-detection of default listeners are only available in Spring Framework 4.1+.

此致

Sam (Spring TestContext Framework的作者)

这篇关于什么是TestExecutionListener,它们是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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