Maven 中服务提供者接口的单元测试

发布于 2024-10-20 11:55:58 字数 758 浏览 2 评论 0原文

最近,我们的开发团队已将项目管理和构建周期从 Ant 构建文件转移到 Maven。到目前为止,我对结果非常满意。由于我仍在掌握 Maven,因此我还没有为不寻常的构建需求准备好答案。

现在我已经创建了一个新项目并决定立即开始进行正确的 Maven 设置。该项目实际上是一个 API,它使用 Java 服务提供者接口 (SPI) 来查找可插入实现。这些实现可以以常规 META-INF/services/factoryname 方式注册自身。 API 完成了大部分工作,但需要实现才能获得真正有用​​的东西。

我现在的问题是:我该如何为此编写单元测试?我想到了两种方法:

  1. 在测试包中创建一个简单的实现,构建该实现,然后将其用于单元测试。这似乎不太适合 Maven 项目生命周期。

  2. 创建一个包含测试实现的单独项目。虽然这意味着我们的 SVN 存储库中的另一个条目,但它感觉像是更干净的方法。该项目可以在主 API 项目的测试周期中作为 jar 来构建和使用。它可以在主项目中列为具有测试范围的依赖项工件。

在这两种情况下都存在一个基本问题...实现需要主 API 进行编译。毕竟它必须有一个工厂类来实现API的抽象工厂。但为了正确构建主要 API、测试阶段等,需要测试实现。这实际上是一个循环依赖的问题。

在 Maven 中处理这个问题的好方法是什么?在 Ant 中,我只是在 API 及其测试之间构建实现。如果这有助于了解的话,我稍微滥用了 SPI 来定位对象模型包而不是单个实现。它不太像 JAXP,而更像是一种注册资源的方式。

Recently our dev team has moved from Ant build files to Maven for our project management and build cycle. So far I'm really happy with the results. Since I'm still getting to grips with Maven, I don't have an answer ready for unusual build requirements.

Now I've created a new project and decided to started to immediately go for a proper Maven setup. The project is really an API that uses the Java Service Provider Interface (SPI) to look up pluggable implementations. The implementations can register themselves in the regular META-INF/services/factoryname manner. The API does the bulk of the work, but an implementation is needed to get anything actually useful.

My question is now: how do I go about writing unit tests for this? Two methods came to mind:

  1. Create a simple implementation in the test packages, have that built and then use this for the unit testing. This doesn't seem to fit weel into the Maven project lifecycle.

  2. Create a separate project containing a test implementation. Although this means another entry in our SVN repository, it feels like the cleaner approach. The project could be built and used as a jar in the testing cycle of the main API project. It could be listed as a dependency artifact with test scope in the main project.

In both cases there's a fundamental issue... The implementation would require the main API to compile. After all, it must have a factory class that implements the abstract factory of the API. But in order to properly build the main API, test phase and all, it would need the test implementation. It's really a matter of cyclic dependencies.

What is a good way to deal with this in Maven? In Ant I would just build the implementation in between the API and its tests. If it helps to know, I'm slightly abusing the SPI to locate bundles of object models rather than a singular implementation. It's less like JAXP and more like a way of registering resources.

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

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

发布评论

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

评论(1

始于初秋 2024-10-27 11:55:58

您可以使用 maven-jar-plugin 生成测试-jar 作为正常构建过程的一部分。通过这种方式,您可以一步生成两个工件。

You can use the maven-jar-plugin to generate the test-jar as part of your normal build process. This way you generate both artifacts in one step.

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