junit4osgi的使用

上传人:汽*** 文档编号:490721484 上传时间:2022-12-28 格式:DOC 页数:9 大小:337.50KB
返回 下载 相关 举报
junit4osgi的使用_第1页
第1页 / 共9页
junit4osgi的使用_第2页
第2页 / 共9页
junit4osgi的使用_第3页
第3页 / 共9页
junit4osgi的使用_第4页
第4页 / 共9页
junit4osgi的使用_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《junit4osgi的使用》由会员分享,可在线阅读,更多相关《junit4osgi的使用(9页珍藏版)》请在金锄头文库上搜索。

1、junit4osgi的使用介绍1. 简介Junit4OSGi继承于junit的简单测试框架,可以在OSGI运行环境中进行测试。针对集成测试或是需要在一个OSGI环境中进行单元测试。结构如下:它分为三个部分:(1) Testsy stem 是真正的应用,被测试的系统(比如:声明式服务部分的测试)。(2) Tests是包含测试代码的bundle,用来测试被测试的系统。(3) Framework是测试框架,包括启动测试Runner和执行测试Runtime,在测试声明式服务中我们使用内嵌的loong框架作为测试框架。2. 使用Junit4OSGi进行测试的操作说明:(1)要对被测试的系统进行测试时,T

2、ests必须是一个新建maven工程,测试的代码必须在src/main/java目录下,而不能在被测试部分srctestjava目录下直接写测试代码(比如要测试声明式服务loong-dservice-impl中的服务注册方面,我们不能在它的srctestjava目录下写测试代码)。(2)所写的测试用例必须继承OSGiTestCase,这个类这个类继承自TestCase,并在它的基础上对OSGI中应用到的一些方法进行了封装,比如getBundle,getContext,getServiceObjects等,方便在写测试代码时获取bundle或是服务的一些信息。(3)在测试之前,要对测试代码所在工

3、程的pom文件中进行如下信息的配置:1)添加测试时所需的依赖。测试时所要用到的bundle通过添加依赖的方式添加到pom文件中,其中loong-junit4osgi、loong-dservice-metadata为必须添加的依赖。具体操作如下:org.trustie.loong.modules.loong-dserviceloong-junit4osgi0.1.0-SNAPSHOTorg.trustie.loong.modules.loong-dserviceloong-dservice-metadata0.1.0-SNAPSHOT2)配置所需的插件,必须配置loong-junit4osgi-

4、plugin插件,如果对声明式服务进行测试时必需配置loong-dservice-plugin插件,具体操作如下:org.trustie.loong.modules.loong-dserviceloong-junit4osgi-plugin0.1.0-SNAPSHOTtestloongorg.trustie.loong.modules.loong-dserviceloong-dservice-plugin0.1.0-SNAPSHOTdservice-bundle其中在configuration中配置的框架为测试执行的框架,目前支持loong和Felix这两种。3)在测试工程中写的TestSui

5、te类也必须在pom中配置,TestSuite类的作用是将所有的测试类都添加进来,配置后就可以执行每一个测试类,TestSuite的写法如下:public class ServiceTestSuite extends TestSuite public static Test suite(BundleContext bc) OSGiTestSuite ot = new OSGiTestSuite(Service Test Suite, bc); ot.addTestSuite(SimpleTest.class); ot.addTestSuite(TestClass.class); ot.addT

6、estSuite(TestProps.class); ot.addTestSuite(TestPropReconfigure.class);return ot;在pom中的配置如下:org.apache.felixmaven-bundle-plugin1.4.3truetest.interfaces$pom.artifactIdtest.suit,test.interfaces.impltest.suit.ServiceTestSuite在配置完上述信息后就可以进行测试了,测试时在bundle的构件过程中执行的,可以直接在eclipse环境下对bundle打包,然后在控制台可以得出测试的结果;

7、也可以在Dos下使用maven命令执行测试,如:mvn install。执行后会在当前bundle的target目录下生成安装的bundle和测试报告。注:在测试时有几个常用的maven命令mvn clean integration-test -DlogService=false 设置日志服务;mvn clean install -Dmaven.test.skip=true 略过测试;mvn clean install -Dmaven.test.failure.ignore=true 当测试出现失败不中断。3. 简单例子演示(1)新建一个maven工程:test.with.junit4osgi

8、,在pom文件中配置好所需的依赖和插件。(2)在srcmainjava目录下定义好服务接口和接口的实现类(也就是构件实现类)。定义的接口和实现类如下:public interface Foo boolean foo();Properties fooProps();Boolean getObject();boolean getBoolean();int getInt();String getString();实现类:public class FooImpl implements Foo private int intProp;private String strProp;private Stri

9、ng strAProp;private int intAProp;private boolean boolProp;public boolean foo() return boolProp;public Properties fooProps() Properties p = new Properties();p.put(intProp, new Integer(intProp);p.put(boolProp, new Boolean(boolProp);p.put(strProp, strProp);p.put(strAProp, strAProp);p.put(intAProp, intA

10、Prop);return p;public boolean getBoolean() return true;public double getDouble() return 2.1;(3)在srcmainresources目录下新建一个元数据文件metadata.xml,具体配置如下:(4)在srcmainjava目录下新建一个package,里面放置测试声明式服务的测试类,测试类都是继承于OSGiTestCase,都是使用断言的形式对注册服务、引用服务等来测试,下面是是一个简单的测试类:public class TestProps extends OSGiTestCase DserviceHelper helper = n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号