---
title: "使用Tycho来构建你的RCP程序(五) —— Plugin Unit Test"
author: "chnic"
date: "2015-04-16T14:35:13Z"
tags: []
categories: ["编程语言"]
slug: "tycho-rcp-plugin-unit-test"
draft: false
lastmod: "2015-04-16T14:35:12Z"
id: 2202368
url: "https://chnic.iteye.com/blog/2202368"
---

通过前面的4篇，我相信你已经成功的用Tycho build出你的第一个RCP可执行程序了。这一篇我们来介绍一下Tycho对Junt的集成。

 

废话不多说，在Tyhco中对Junit的集成是通过tycho-surefire-plugin插件来实现的。从插件的名字上来看很像Maven里的surefire插件。但其实Tycho的surefire和Maven的surefire还是有一些差别的，这里的差别主要体现在生命周期上。tycho-surefire-plugin插件是在integration-test phase执行的，而maven-surefire-plugin插件是在test phase执行的。也许你对integration-test这个phase不太了解，其实这个pahse是位于我们经常做的package和install之间的。

写道

Package -&gt; pre-integration-test -&gt; integration-test -&gt; post-integration-test -&gt; verify -&gt; install -&gt; deploy

 

对tycho-surefire-plugin插件有了一定的了解之后，我们就可以开始创建我们的Junit测试项目了。我们应当创建什么样的项目用来RCP的测试呢？针对于plugin项目的测试，在这里推荐的是Fragment Project。你也许会好奇为什么选择Fragment项目？原因有两点：

1\. Fragment可以访问他的主plugin中的代码而不必去导出他们，因为他们本身就处在一个plugin当中，而且Fragment中的代码其实是是用主plugin的classloader来加载的。

2\. 因为从外部来看Fragment和和主plugin项目其实是一个plugin，他们用的是一套的classloader，所以假如你的测试class和你主class是在一个包内，在我们的测试class当中甚至可以直接访问主class的私有方法。

 

我们通过eclipse的项目创建向导创建一个Fragment项目。因为我们测试的plugin是我们之前创建的com.chnic.tycho.mail.plugin，因此在这里我们的fragment的项目名取名为com.chnic.tycho.mail.plugin.test。在接下来的host plugin指定的对话框中我们指定com.chnic.tycho.mail.plugin作为host plugin

![](http://dl2.iteye.com/upload/attachment/0107/6481/87aca670-13f3-39e2-858a-0be3b5bb71a9.png)  
 

项目创建完成之后我们还是convert to Maven project。因为这是一个Junit测试项目，因此在这里我们的packaging设置成为eclipse-test-plugin

![](http://dl2.iteye.com/upload/attachment/0107/6485/b6c90bb0-24db-37ff-8bd7-bd8da0051905.png)  
 

很显然，生成的pom文件会有错误，因为他识别不出来eclipse-test-plugin这种packaging方式。我们依然需要让他继承我们的parent项目的pom。

 

```
<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>com.chnic.tycho.mail</groupId>
    <artifactId>com.chnic.tycho.mail.parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../com.chnic.tycho.mail.parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>com.chnic.tycho.mail.plugin.test</artifactId>
  <packaging>eclipse-test-plugin</packaging>
</project>
```

 

转化成Maven项目之后，我们来添加一个junit test case。我们创建一个和host plugin一模一样的包名，然后在其之内创建一个PerspectiveTest测试类

 

```
package com.chnic.tycho.mail.plugin;

import static org.junit.Assert.*;

import org.junit.Test;

public class PerspectiveTest {
	
	@Test
	 public void testID() {
	  assertEquals("com.chnic.tycho.mail.plugin.perspective", Perspective.ID);
	 }
}
```

 

创建完成之后我们会发现测试类报错了，那是因为我们没有把junit加到fragment项目的Require-Bundle里。ctrl+1修复编译错误。

 

```
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test
Bundle-SymbolicName: com.chnic.tycho.mail.plugin.test
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Nick
Fragment-Host: com.chnic.tycho.mail.plugin;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit
```

 

所有的工作完成之后，我们要做的就是吧我们的测试项目添加到我们的聚合项目com.chnic.tycho.mail.build中去。

 

```
<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>com.chnic.tycho.mail</groupId>
    <artifactId>com.chnic.tycho.mail.parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../com.chnic.tycho.mail.parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>com.chnic.tycho.mail.build</artifactId>
  <packaging>pom</packaging>
  
  <modules>
  	<module>../com.chnic.tycho.mail.feature</module>
  	<module>../com.chnic.tycho.mail.plugin</module>
  	<module>../com.chnic.tycho.mail.product</module>
  	<module>../com.chnic.tycho.mail.updatesite</module>
  	<module>../com.chnic.tycho.mail.plugin.test</module>
  </modules>
</project>
```

 

Build我们的com.chnic.tycho.mail.build项目，我们会发现Maven在build的时候控制台上多出了如下的信息

写道

\-------------------------------------------------------  
T E S T S  
\-------------------------------------------------------  
Running com.chnic.tycho.mail.plugin.PerspectiveTest  
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.chnic.tycho.mail.plugin.PerspectiveTest  
testID(com.chnic.tycho.mail.plugin.PerspectiveTest) Time elapsed: 0 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

\[INFO] All tests passed!

 

控制的信息告诉我们，test case已经被执行，并且case是测试通过的。由于我们的测试fragment项目只会在integration-test phase被执行，并不会被打包到最终的product中，你在product的plugins的文件夹中也找不到相关的jar文件。

 

在这里还要额外的说一句，由于Tycho的surefire plugin是支持基于UI和不基于UI的测试的，如果你的test case是一个基于UI的test case，那么你需要在POM文件中，显示的指定useUIHarness为true。

```
<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>com.chnic.tycho.mail</groupId>
    <artifactId>com.chnic.tycho.mail.parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../com.chnic.tycho.mail.parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>com.chnic.tycho.mail.plugin.test</artifactId>
  <packaging>eclipse-test-plugin</packaging>
  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.eclipse.tycho</groupId>
        <artifactId>tycho-surefire-plugin</artifactId>
        <version>${tycho.version}</version>
        <configuration>
          <useUIHarness>true</useUIHarness>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
```

 

至此，我们也完成了Tycho对Junit的集成。com.chnic.tycho.mail.plugin.test项目代码，可以在附件中找到。在下一篇中会介绍Tycho的一些其他的有用的配置。