OSGi enRoute – 2.3_9 – 集成测试
你将在本章学到什么
在本章中我们将会创建一个OSGi集成测试。我们将会使用bnd-testing-maven-plugin来设置一个运行时环境,然后在OSGi中运行我们的JUnit测试。
确保你位于顶层文件夹中:
1 |
$ cd ~/workspaces/osgi.enroute.examples.eval |
创建一个Test Bundle
我们将会首先创建一个test bundle。Test bundle中包含一个或多个JUnit测试用例。每个Bundle将会在Test-Case manifest数据头中列出它的测试类。Bnd运行时支持的Tester会查找带有这个数据头的Bundle,然后自动运行这些测试。
在本例中,我们准备定义一个验证Eval Service的测试。
创建 pom
让我们来创建pom文件。
1 2 3 4 |
osgi.enroute.examples.eval $ mkdir test osgi.enroute.examples.eval $ cd test test $ vi pom.xml // get the content |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.examples.eval</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>osgi.enroute.examples.eval.test</artifactId> |
我们需要API Bundle和JUnit作为依赖项。我们使用OSGi enRoute封装过的JUnit版本,因为普通的JUnit中没有OSGi元数据而很多封装版存在问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<dependencies> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.examples.eval.api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.junit.wrapper</artifactId> <version>4.12.0</version> </dependency> </dependencies> </project> |
源代码
集成测试Bundle的源码必须放在src/main/java文件夹中,而不是test文件夹中,这可能会给大家带来一点点困惑。如果你意识到我们正在构建一个Bundle,而在src文件夹中的test代码不会最终出现在Bundle中,那么这样做是有意义的。
1 2 3 |
test $ mkdir -p src/main/java/osgi/enroute/examples/eval/test test $ vi src/main/java/osgi/enroute/examples/eval/test/EvalTest.java //获取下面章节的内容 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package osgi.enroute.examples.eval.test; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import junit.framework.TestCase; import osgi.enroute.examples.eval.api.Eval; public class EvalTest extends TestCase { BundleContext context = FrameworkUtil.getBundle(EvalTest.class).getBundleContext(); public void testEval() throws Exception { ServiceReference<Eval> ref = context.getServiceReference(Eval.class); assertNotNull("No such service", ref); Eval eval = context.getService(ref); assertNotNull("Service object init error", eval); assertEquals( 7.0D, eval.eval("1+6")); } } |
注意bnd运行时若要支持测试需要设置Test-Cases manifest数据头。因此我们需要一个bnd.bnd文件。
1 2 |
test $ vi bnd.bnd //获取下面章节的内容 |
1 2 3 4 |
Bundle-Description: \ Integration Test bundle for the Eval service Test-Cases: ${classes;NAMED;*Test} |
本例中最重要的技巧是使用了classes宏。它将会列出所有以Test结尾类。
安装
现在我们已经创建了一个测试bundle,因此我们需要安装它。
1 2 |
test $ mvn install ... |
你应该也将这个工程添加到parent pom中。
集成测试工程
集成测试需要一个运行时环境。我们已经使用bndrun文件来为我们的应用程序创建了一个运行时环境。我们可以使用类似的一个或多个bndrun文件来执行集成测试。
如果你需要在不同的平台上进行测试,则多个环境会非常有用。
创建集成工程和pom
1 2 3 4 5 |
test $ cd .. osgi.enroute.examples.eval $ mkdir integration-test osgi.enroute.examples.eval $ cd integration-test integration-test $ vi pom.xml //获取下面章节的内容 |
我们的pom内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.examples.eval</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>..</relativePath> </parent> <artifactId>osgi.enroute.examples.eval.integration-test</artifactId> <packaging>pom</packaging> <build> <plugins> |
这个插件将会遍历bndrun文件并且执行其中的测试。
1 2 3 4 5 6 7 |
<plugin> <groupId>biz.aQute.bnd</groupId> <artifactId>bnd-testing-maven-plugin</artifactId> <version>3.4.0-SNAPSHOT</version> <configuration> <resolve>true</resolve> <bndruns> |
下面是bndrun文件的相对路径。我们将会创建一个带有parsii provider的运行时用于测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<bndrun>parsii.bndrun</bndrun> </bndruns> <targetDir>.</targetDir> </configuration> <executions> <execution> <goals> <goal>testing</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
为了解析我们的工程,我们需要列出需要解析的依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<dependencies> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.examples.eval.parsii.provider</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.examples.eval.test</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.enroute.pom.distro</artifactId> <version>2.0.0</version> </dependency> </dependencies> </project> |
parsii.bndrun文件
在bnd-testing-maven-plugin中的bndruns部分,我们列出了parsii.bndrun文件。我们因此需要创建它:
1 2 |
integration-test $ vi parsii.bndrun //获取下面章节的内容 |
它的内容与我们在bndrun工程中创建的bndrun文件很类似。
1 2 3 4 5 6 7 8 9 |
-standalone: -plugin.examples.eval = \ aQute.bnd.repository.maven.pom.provider.BndPomRepository; \ snapshotUrls=https://oss.sonatype.org/content/repositories/osgi/; \ releaseUrls=https://repo1.maven.org/maven2/; \ pom=${.}/pom.xml; \ name=examples.eval; \ location=${.}/target/cached.xml |
我们希望测试parsii provider。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-runrequires: \ osgi.identity;filter:='(osgi.identity=osgi.enroute.examples.eval.parsii.provider)',\ osgi.identity;filter:='(osgi.identity=osgi.enroute.examples.eval.test)' -runfw: org.eclipse.osgi;version='[3.10.100.v20150529-1857,3.10.100.v20150529-1857]' -runtrace: true -runee: JavaSE-1.8 -resolve.effective: resolve, active -runsystempackages.eqnx: javax.script -runsystemcapabilities.dflt: ${native_capability} |
与之前的bndrun文件类似,我们需要解析该文件并查找bundle。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
integration-test $ mvn install ... -runbundles: \ org.apache.felix.configadmin; version='[1.8.8,1.8.9)',\ org.apache.felix.scr; version='[2.0.2,2.0.3)',\ org.eclipse.equinox.metatype; version='[1.4.100,1.4.101)',\ org.osgi.service.metatype; version='[1.3.0,1.3.1)',\ osgi.enroute.examples.eval.parsii.provider; version='[1.0.0,1.0.1)',\ osgi.enroute.examples.eval.test; version='[1.0.0,1.0.1)',\ osgi.enroute.hamcrest.wrapper; version='[1.3.0,1.3.1)',\ osgi.enroute.junit.wrapper; version='[4.12.0,4.12.1)' integration-test $ vi parsii.bndrun // update the -runbundles integration-test $ mvn install ... Test parsii.bndrun Tests run : 1 Passed : 1 Errors : 0 Failures : 0 ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ... |
我们学到了什么?
在本章中,我们创建了一个OSGi集成测试。这需要一个带有Test-Case Manifest数据头集的test bundle。它还需要一个integration-test工程来运行测试。这个工程可以通过定义多个bndrun文件并将它们列在bnd-testing-maven-plugin的Configuration部分中来运行多7个集成测试。

