【问题标题】:OSGi Fragment using Spring Integration Testing使用 Spring 集成测试的 OSGi 片段
【发布时间】:2014-01-04 06:34:24
【问题描述】:

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

问题定义

模板应用

  • 一个包含业务逻辑的 OSGi 包。使用 Spring DM 导出/引用一些服务
  • 一个用于集成测试的 OSGi 片段。测试应该能够注入在片段应用程序上下文中定义的辅助测试 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" }) 会导致FileNotFoundException 关于context.xml,它存在...... 调试 XmlBeanDefinitionReader 证实了我的疑问:SpringJUnit4ClassRunner 正在尝试从其 OSGi 类路径加载应用程序上下文,并且无法访问我的捆绑资源。

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

提前感谢您的帮助。

ps:我查看了Spring DM Test,但没有发现任何相关内容。

示例

捆绑清单

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

集成测试片段清单

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);
    }

}

【问题讨论】:

    标签: java spring osgi integration-testing spring-dm


    【解决方案1】:

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

    【讨论】:

    • 感谢您的评论。我也考虑过,但发现对于通用集成测试来说真的很复杂。真的没有其他解决方案了吗?没有 Meta Spring-Junit Runner 能够处理分离的 OSGi 类路径?谢谢
    • 据我所知。不过,Pax 考试并没有那么复杂。最新版本甚至允许在许多情况下进行测试
    猜你喜欢
    • 2016-07-25
    • 2014-04-29
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多