0

我在 ServiceMix Container 中部署了一个 OSGI Bundle (B)。B 还充当 OSGI 端点。我们可以看到以下行(来自我的 ApplicationContext.xml)

<osgi:service id="SampleManagementService" ref="ManagementService" interface="com.abc.webservice.xyz.ISampleManagementService"/>

现在,我在同一个 servicemix 容器中部署了另一个 Bundle (C),并且 C 想要使用从 B 公开的 OSGI 服务。所以我在 Bundle C 的 applicationContext 中添加了以下行。

<osgi:reference id="SampleManagementService" interface="com.abc.webservice.xyz.ISampleManagementService"/>

到目前为止,一切都很好。现在我在 Bundle C 中编写了一个 JUNIT 测试用例,它尝试在执行测试用例之前加载它的 applicationContext.xml。这是我的测试类的片段:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/META-INF/spring/camel-context.xml"})
public class TimerTest {
   //some logic to execute the test.
}

每当我尝试运行这个单元测试用例时,我都会得到java.lang.IllegalStateException:。这太笼统了,无法为我提供有关该问题的任何线索。

这是我在控制台中看到的异常的堆栈跟踪:

main] TestContextManager ERROR Caught exception while allowing        TestExecutionListener        [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1526ea43] to prepare test instance [com.abc.sms.webservice.test.TimerTest@2ddddf8e]
java.lang.IllegalStateException: Failed to load ApplicationContext
at   org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308) [spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at  org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)[junit-4.7.jar:]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)[junit-4.7.jar:]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)[junit-4.7.jar:]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)[junit-4.7.jar:]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)[junit-4.7.jar:]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)[junit-4.7.jar:]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)[junit-4.7.jar:]
at  org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)[junit-4.7.jar:]
at  org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)[spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE]

我不确定是什么导致了这个问题。我尝试从 POM.xml 中删除 JARS,但这也有帮助。我还在 Stackoverflow 中寻找了类似的问题,但其中大多数都是通过更改我的测试类中的 Application-Context.xml 的类路径来回答的,这肯定不是我的情况。一旦我从 Bundle C中删除<osgi:reference> ,我的测试就会成功运行。

任何建议都会有所帮助。谢谢。

4

2 回答 2

1

如果你有一个 OSGI 服务参考(你这样做是因为你使用了 osgi:reference spring 标签),你在评估你的单元测试时需要那个参考。由于您正在运行的单元测试没有 OSGI 运行时,因此 osgi:reference 失败。

最好的办法是使用不尝试使用 OSGI 运行时的 /META-INF/spring/camel-context.xml 的替代版本来编写单元测试。

在不知道您使用的是哪个版本的 spring 的情况下,3.1 中的配置文件也可以提供帮助。

于 2014-01-03T23:08:20.260 回答
1

有一个名为 Pax Exam 的项目可用于 OSGi 容器中的 JUnit。由于 ServiceMix 基于 Top of Karaf,您可能想知道 Pax Exam 还支持将 Karaf 容器作为运行时(从 3.4 版开始)。您将在 OPS4j找到有关如何使用 Pax 考试的更多详细信息。您可能还对如何测试 Camel OSGi 感兴趣, Testing Camel JPA Routes上有一篇博文。即使博客文章使用了 Blueprint,它也可以很容易地转移到 spring。

于 2014-01-04T17:19:19.647 回答