【问题标题】:IllegalStateException while launching JUnit test with OSGi使用 OSGi 启动 JUnit 测试时出现 IllegalStateException
【发布时间】:2012-08-17 20:44:34
【问题描述】:

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

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 安装详细信息”,核心运行时版本为 3.8.0.v20120521-2346,在 junit 所需的正确范围内([3.3.0,4.0.0))。

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

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

【问题讨论】:

  • 你是在使用junit进行单元或服务间集成测试吗?对于单元测试,您应该不依赖于 OSGi 子系统。对于后者,有几种选择...
  • 感谢您的回答。我当前尝试运行的测试仅调用主机包的内部例​​程。我没有尝试进行服务间集成,但我不确定它会对我当前的问题产生多大影响。确实,我的错误似乎来自尝试使用核心运行时但没有找到它的 eclipse junit 插件(“org.eclipse.pde.junit.runtime”)。
  • 您是使用 junit 运行器还是使用“Junit Plug-in Test”运行器运行 junit 测试?您不应该在简单的 Junit 测试中参与 osgi 环境。
  • 我真的很抱歉,因为我可能意识到我的第一个解释并不好。我正在使用“Junit Plug-in Test”运行程序运行它。我正在测试一个 OSGi 应用程序,并且我使用了一个由我想要测试的包托管的测试片段。我的测试不完全涉及服务间集成,但我需要在 OSGi 环境中。我已经多次以这种方式测试过 OSGi 应用程序,没有任何问题。
  • 好吧,我不使用激活器,因为我更喜欢 DS 注入。对于资源定位,您可以使用 public void activate(ComponentContext context) 版本的 activate 方法向您的服务注入模拟上下文。

标签: java eclipse junit osgi


【解决方案1】:

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

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

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

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

然后,在你的单元测试准备中,你可以简单地声明一个 Mock 并使用 bind 方法注入它。

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

对于包间集成和系统测试,您可以选择Junit plugin test 或查看Pax Exam

【讨论】:

  • 非常感谢您的回答,这真的很有趣,并添加了几个有意义的点。对于配置本身,我对我的“正常”OSGi 运行使用完全相同的配置(即具有相同启动顺序的相同插件)并且我没有收到任何错误。此外,未解决的依赖关系与 eclipse 运行时本身有关,这很奇怪。一旦确定没有其他外部问题,我会尽快接受您的回答(如果还有其他问题,我会在此处发布,以便我们将知识集中在一个独特的帖子中)。
猜你喜欢
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 2017-11-21
  • 2017-03-20
  • 2023-03-06
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
相关资源
最近更新 更多