java - 使用 Spring 集成测试的 OSGi 片段

标签 java spring osgi integration-testing spring-dm

由于到目前为止我还没有找到任何相关答案,所以我发布了这个问题。 我不知道如何使用 Spring-Dynamic 模块运行集成测试。

问题定义

模板应用

  • 一个包含业务逻辑的 OSGi 包。使用Spring DM导出/引用一些服务
  • 用于集成测试的一个 OSGi 片段。测试应该能够注入(inject)在片段应用程序上下文中定义的辅助测试 bean(使用 META-INF/spring 下的 xml 文件)以及导出的 OSGi 服务(从 API 包)

版本

  • OSGi 3.6
  • Spring框架3.1.0
  • Spring 动态模块 1.2.1

问题

  • 如何运行集成测试来了解 Spring 应用程序上下文和公开的 OSGi 服务? 使用 @RunWith(SpringJUnit4ClassRunner.class) 和 @ContextConfiguration(locations = { "classpath:META-INF/spring/context.xml"}) 会导致有关 context.xml 的 FileNotFoundException,该异常存在... 调试 XmlBeanDefinitionReader 证实了我的疑虑:SpringJUnit4ClassRunner 正在尝试从其 OSGi 类路径加载应用程序上下文,并且无法访问我的捆绑资源。

请问您如何最好地解决这个问题?

非常感谢您提前提供的帮助。

ps:我看了一下Spring DM Test但没有找到任何相关内容。

示例

捆绑 list

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorldDM
Bundle-SymbolicName: HelloWorldDM
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.core.runtime,
 ContactDAO;bundle-version="1.0.0",
 org.springframework.osgi.extensions.annotations;bundle-version="1.2.1"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: ch.xxx.test.contact

集成测试片段 list

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorldTest
Bundle-SymbolicName: HelloWorldTest
Bundle-Version: 1.0.0.qualifier
Fragment-Host: HelloWorldDM;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.junit4;bundle-version="4.8.1",
 org.springframework.spring-test;bundle-version="3.1.0",
 org.springframework.spring-context;bundle-version="3.1.0",
 org.springframework.spring-beans;bundle-version="3.1.0",
 org.springframework.osgi.test;bundle-version="1.2.1",
 org.springframework.spring-core;bundle-version="3.1.0"

片段应用程序上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  
  <bean class="org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor"/>
  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  
</beans>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/context.xml" })
public class TestCase {
    
    private BitemporalityService bitemporalityService;

    @ServiceReference
    public void setBitemporalityService(BitemporalityService service) {
        this.bitemporalityService = service;
    }

    @Test
    public void dummy(){
        System.err.println("in Test: ");
        assertNotNull(bitemporalityService);
    }

}

最佳答案

看看pax exam 。它启动一个 osgi 容器并在其中运行您的测试。它是 osgi 测试事实上的标准框架,并在 osgi itests 的许多开源项目中使用。请参阅https://github.com/apache/karaf/tree/karaf/itests

关于java - 使用 Spring 集成测试的 OSGi 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20618207/

相关文章:

java - 将对象附加到现有文件

java - 如何修复 Kafka Producer 示例的 NoClassDefFoundError?

spring - ApacheConnectorProvider 与 Jersey for SSL 抛出错误

java - 如果花费太多时间,如何取消 AsyncRestTemplate HTTP 请求?

java - bundle 内的 bundle

Java - Collections.sort() 性能

java - 轻松使用 CursorLoader

java - withings API 的 Oauth 签名失败

design-patterns - OSGi 应用程序设计 - 我在滥用服务框架吗?

java - 在嵌入 Android 的 Knopflerfish 上启动 Bundle 不起作用