OSGi 中的依赖注入与测试
对于一个新项目,我正在研究 OSGi 在依赖注入方面必须提供的功能,我有点喜欢 iPOJO(纯粹基于注释,而不是支持 xml)。
然而,从测试的角度来看,蓝图可能更好,因为对于不同的测试用例(功能测试),重写蓝图配置可能就足够了,并且会立即注入其他服务。
您对这个话题有何看法? 我是否可以放弃基于 XML 的蓝图(我讨厌 XML)而转而使用 iPOJO,而不牺牲测试方面的灵活性?
For a new project I'm looking at what OSGi has to offer in terms of dependency injection, and I kinda like iPOJO (pure annotation-based, not the xml backed).
However, from a testing perspective, Blueprint might be better, since then for different test cases (functionality testing) it might be enough to rewrite the blueprint configuration and there would be an other service injected right away.
What are your thoughts on this subject?
Can I abandon XML-based Blueprint (I loathe XML) in favor of iPOJO without sacrificing flexibility in terms of testing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。你可以。我不认为你会牺牲任何东西。
事实上,iPOJO 比 Blueprint 更强大,它支持“字段注入”、“服务生命周期控制”和“配置管理”等功能,而 Blueprint 则不支持这些功能 (参考)。
不过,如果重要的话,蓝图是 OSGi 企业规范的一部分。
我没有使用过 Blueprint,但只是查看了它的规范 - 除非您是 java beans 人员,否则我会远离它。另外,我更喜欢 iPOJO 而不是 DS,因为它在很多情况下似乎更聪明,并且只做正确的事情。
Yes. You can. I don't think you will sacrafice anything.
In fact, iPOJO is more powerful then blueprint, it supports things like "Field injection", "Service lifecycle control", and "Configuration admin" - which Blueprint does not (reference).
Blueprint, however, is part of the OSGi Enterprise Specification, if that matters.
I have not worked with Blueprint, but just looking at the specification for it - unless you are a java beans guy, I would stay away. Also, I prefer iPOJO over DS, as it seems to be a lot smarter in many cases, and just does the right thing.
关于 iPOJO 单元测试,您可以使用构造函数(或构造函数注入)来注入模拟服务并有效地测试组件的行为:
因此有两种选择:
您需要使用模拟
注入,然后您只需使用模拟对象调用构造函数
如果您尝试进行集成测试,您可以仅使用 pax exam 并部署您的包(除了 iPOJO 之外)。
Regarding iPOJO unit testing, you can use constructor (or constructor injection) to inject mock services and effectively test the behavior of your component:
So two choices:
you need using mocks
injection, and then you just call the constructor with mock objects
If you trying to do integration testing, you can just use pax exam and deploy your bundle (in additional to iPOJO).