java - 使用 OSGi 启动 JUnit 测试时出现 IllegalStateException

标签 java eclipse junit osgi

我的一个项目遇到一些奇怪的问题。我正在尝试在 OSGi 环境中运行一些 JUnit 测试(测试位于 osgi 包托管的片段中;操作是使用 eclipse 中的“JUnit 插件测试”启动器启动的)。当我尝试启动测试时,出现以下错误:

java.lang.IllegalStateException: Unable to acquire application service. Ensure that the    org.eclipse.core.runtime bundle is resolved and started (see config.ini).
    at  org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:74)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1383)
An error has occurred. See the log file

在上面的日志中我还得到:

!ENTRY org.eclipse.osgi 2 0 2012-08-22 13:53:24.058
!MESSAGE One or more bundles are not resolved because the following root constraints are not resolved:
!SUBENTRY 1 org.eclipse.osgi 2 0 2012-08-22 13:53:24.059
!MESSAGE Bundle reference:file:/C:/.../plugins/org.eclipse.pde.junit.runtime_3.4.200.v20120530-1435.jar was not resolved.
!SUBENTRY 2 org.eclipse.pde.junit.runtime 2 0 2012-08-22 13:53:24.059
!MESSAGE Missing required bundle org.eclipse.core.runtime_[3.3.0,4.0.0).

但是,org.eclipse.core.runtime 似乎可用(Eclipse 运行没有任何问题,我可以使用 OSGi 运行启动该项目)。根据“Eclipse Installation Details”,核心运行时的版本为 3.8.0.v20120521-2346,位于 junit 所需的正确范围内 ([3.3.0,4.0.0))。

我还有另一个相同的 Eclipse(相同版本等...它来自相同的 .zip 存档),我可以在其中为另一个项目运行相同类型的测试。我检查了配置,但没有发现任何差异。因此我目前无法理解是什么导致了这个问题。

提前非常感谢您提供任何可以帮助我解决这个问题的想法,

最佳答案

您的 JUnit 插件测试配置中似乎缺少一些包。您可以使用Plugins页面的Validate Plug-ins功能检查您的配置是否完成。请注意:我遇到过一些使用此方法出现依赖蔓延的情况,通常来自一些“无辜的”Eclipse 运行时插件,而这些插件本来就不应该存在。

也就是说,如果您使用 DS(声明性服务),您可以消除对 OSGi API 的所有依赖,因此可以使用 vainilla JUnit 来测试您的代码。 DS 的一个特别隐藏的宝藏是服务激活签名 public void activate(ComponentContext context),它允许您将 ComponentContext 注入(inject)到您的服务中。使用模拟(我们使用 Mockito),您可以模拟任何所需的调用,例如加载资源。

同样,您可以模拟所有服务,因为它们可以通过 osgi-inf 描述符中声明的生命周期方法(绑定(bind)、取消绑定(bind))注入(inject)(我们使用 Equinox):

<reference interface="com.service.itf" name="UsefulService"  cardinality="1..1" policy="static" bind="onDependencyServiceUp"  unbind="onDependencyServiceDown"/> 

然后,在单元测试准备中,您可以简单地声明一个 Mock 并使用绑定(bind)方法注入(inject)它。

这将使您在 OSGi 中的测试工作变得更加容易,因为您不必为每个测试创建和维护捆绑配置,同时,vainilla Junit 测试运行得更快。

对于 bundle 间集成和系统测试,您可以选择 Junit 插件测试 或查看 Pax Exam .

关于java - 使用 OSGi 启动 JUnit 测试时出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12072915/

相关文章:

java - Android Studio SQLiteOpenHelper "DATABSE has no column named NAME"

eclipse - 在 Eclipse CDT 中浏览 Nginx 源代码

spring-mvc - RestAssuredMockMvc 连接到 http ://localhost:8080 refused

Spring、spring Batch、hibernate 和 JUnit 以及多个集成测试的初始化

java - Hibernate 中的多个事务

java - 我可以在构造函数中传递多个 Super 调用吗?

Eclipse 插件信任权威问题

java - JUnit 5 中@RuleChain 的等价物是什么?

java - 级联不适用于使用 Spring Data JPA 存储库进行删除

java - 在 Eclipse 中获取 Xively 数据流