将 Spring 集成测试放在不同的类和包中

发布于 2024-07-14 09:00:03 字数 194 浏览 8 评论 0原文

我正在使用 AbstractTransactionalSpringContextTests 来运行 spring 集成测试。 spring 上下文仅加载一次,然后运行所有测试。 如果我希望我的测试位于多个类和包中,我该如何做同样的事情。

当然,对于我的所有测试(在所有类和包中),spring 上下文应该只加载一次,而不是每个类或包加载一次。

I am using AbstractTransactionalSpringContextTests to run spring integrations tests.
The spring context is loaded just once and then all the tests are run.
How do I do the same if I want my tests to be in many classes and packages.

Of course, the spring context should be loaded just once for all my tests (in all classes and packages), and not once per class or package.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心激荡 2024-07-21 09:00:03

作为 AbstractSingleSpringContextTests 的 JavadocAbstractTransactionalSpringContextTests 的超类之一)状态:

此类将根据上下文键缓存上下文:通常配置位置字符串数组描述构成上下文的 Spring 资源描述符。 除非测试调用 setDirty() 方法,否则不会重新加载上下文,即使跨此测试的不同子类也是如此。 如果您的上下文构建速度很慢,例如,如果您使用 Hibernate 并且加载映射所花费的时间是一个问题,那么这尤其有用。

您的上下文已被缓存,因此在同一类加载器中运行的所有其他测试(即在同一测试运行期间运行的所有其他测试)将使用缓存的上下文。 您无需进行任何额外的设置 - 应该已经为您完成了。 如果您对此不确定,或者希望排除故障,只需打开 org.springframework 的日志记录,您应该会看到大量有关上下文加载时间、哪个文件、如何加载的有用日志记录; ETC。

As the Javadocs for AbstractSingleSpringContextTests (one of the superclasses of AbstractTransactionalSpringContextTests) state:

This class will cache contexts based on a context key: normally the config locations String array describing the Spring resource descriptors making up the context. Unless the setDirty() method is called by a test, the context will not be reloaded, even across different subclasses of this test. This is particularly beneficial if your context is slow to construct, for example if you are using Hibernate and the time taken to load the mappings is an issue.

Your context is cached, so all other tests that run within the same classloader (i.e. - all your other tests that run during the same test run) will use the cached context. You don't need to do any extra setup - should be done for you already. If you're not sure about this, or wish to troubleshoot, simply turn on the logging for org.springframework and you should see plenty of helpful logging about when the context is being loaded, which file, how; etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文