我可以让 Surefire 仅执行一次昂贵的设置/拆卸吗?

发布于 2024-12-04 14:51:43 字数 519 浏览 0 评论 0原文

我有一系列单元测试,所有测试都需要与 Apache Zookeeper 服务器通信。显然,如果我有一个用于模拟服务器连接的库(或者一种简单的方式来滚动我自己的连接),那将是最佳的,但据我所知,目前还不存在,而且引擎盖下有足够的移动部件,我犹豫不决手动尝试一下。

因此,到目前为止,我们的解决方案是在本地建立一个虚拟服务器,执行测试,然后最后将其拆除。这种方法是有效的,但据我所知,没有简单的方法可以告诉 Surefire 在整个测试运行中执行一次操作 - 最多,我有 @BeforeClass@AfterClass< /code> 装饰品。

Zookeeper 初始化过程将原本需要几毫秒的测试运行变成每个类的几秒钟,这对于整个项目来说需要几分钟。如果我可以设置测试,以便它启动服务器,运行每个测试,然后将其拆除,我想我会看到至少一个数量级的加速。

有一个简单的修复方法吗?我是否以错误的方式处理这个问题?如果有一个现有的模拟动物园管理员库,或者一个简单的方法来推出我自己的库,那也是一个很好的解决方案。

I have a series of unit tests that all need to talk to an Apache Zookeeper server. Obviously, if I had a library for mocking server connections (or an easy way to roll my own) that would be optimal, but as far as I can tell none exists at this point, and there's enough moving parts under the hood that I hesitate to try it by hand.

So, our solution so far has been to stand up a dummy server locally, perform the tests, then tear it down at the end. This kind of works, though as far as I can tell there's no easy way to tell Surefire to do something once per entire test run -- at best, I have @BeforeClass and @AfterClass decorations.

The Zookeeper initialization process is turning what should be several-millisecond test runs into several seconds per class, which works out to several minutes for the whole project. If I could set up the tests so that it stands up a server, runs each test, then tears it down, I think I'd see a speedup of at least an order of magnitude.

Is there a simple fix? Am I going about this the wrong way? If there's an existing mock-zookeeper library, or a simple way to roll my own, that's a fine solution too.

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

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

发布评论

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

评论(3

我ぃ本無心為│何有愛 2024-12-11 14:51:44

听起来像是集成测试。考虑“故障安全”而不是“万无一失”,并在额外的生命周期阶段中执行昂贵的操作来设置和拆除集成测试。

Sounds like an integration test. Look at 'failsafe' instead of surefire, and do the expensive stuff in the additional lifecycle phases for setup and teardown of integration tests.

2024-12-11 14:51:44

如果您可以选择使用 TestNG,则可以使用 @BeforeSuite。

If using TestNG is an option to you, you could use @BeforeSuite.

佞臣 2024-12-11 14:51:44

您可能需要检查 AbstractSingleSpringContextTests 春季课程。该类为所有子类加载Spring上下文,并且仅在调用setDirty()方法时才重新加载它。

不完全是您想要的,但我确信您可以复制该模式并使用它来启动模拟动物园管理员服务器。

You might want to check the AbstractSingleSpringContextTests class in Spring. This class loads a Spring context for all the subclasses and only reloads it if the method setDirty() is called.

Is not exactly what you want, but I'm sure you can copy the pattern and use it to start a mock zookeeper server.

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