简体   繁体   中英

Dependency injection in OSGi vs testing

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?

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.

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:

  • either you have an additional constructor to inject the dependencies you need using mocks
  • or your component is using constructor 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM