Unit Tests for Eclipse Plugins.

Maven projects typically have separate test source directories in the same project. The Eclipse convention, however, is to have a separate test bundle (often a fragment of the host/target plugin with the suffix ".tests").
Fragments allow to access all classes of the main plug-in without exporting them. If you use the same package name for your unit test class and the class under test you can even access package private methods.
Eclipse Tycho introduces new eclipse-test-plugin packaging type to represent such projects. Build behavior is like regular Eclipse plugins, but these are treated specially at test-time.

1. Create a Sample Unit Test:
Create a new Fragment Project named com.test.tycho.plugin.tests. Set the Host Plug-in ID to com.test.tycho.plugin.




2. Add to master POM:
Convert above fragment plugin to maven project  and don't foget to set Packaging to eclipse-test-plugin . Add it to master POM file.
See the fragment plugin's POM file it should be like:

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

3. Create a new JUnit Test:
Create simple Test class in your fragment plugin.

package com.test.tycho.plugin;

import static org.junit.Assert.*;
import org.junit.Test;

public class MainTest {

 @Test
 public void test1() {
  assertEquals(true, 1==1);
 }
}

Build your product and your tests will automatically run as part of your build process.


4. Create Jacoco Coverage File (**.exec) form Unit Test to use it for sonar analysis:

A. You just need to add the following section in the pluginManagement section of the Parent POM.
<!-- Testing -->
<plugin>
   <groupId>org.eclipse.tycho</groupId>
   <artifactId>tycho-surefire-plugin</artifactId>
   <version>0.19.0</version>
   <configuration>
      <useUIHarness>false</useUIHarness>
      <includes>
         <!-- which files you want to include, you can remove it if all file you want to run -->
         <include>**/*Test.java</include>
      </includes>
      <!-- Kill test JVM if tests take more than 10 minutes (600 seconds) to finish -->
      <forkedProcessTimeoutInSeconds>600</forkedProcessTimeoutInSeconds>
      <!-- It will ignore if any test fails and continue the build -->
      <testFailureIgnore>true</testFailureIgnore>
   </configuration>
</plugin>


B. 
And then provide the following profile.

<!-- This profile is used to gather code coverage for Sonar -->
<profile>
  <id>codeCoverage</id>
  <properties>
    <!-- Properties to enable jacoco code coverage analysis -->
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>../your folder/jacoco.exec</sonar.jacoco.reportPath>
  </properties>
 
  <build>
    <plugins>
      <!-- Enabling use of jacoco -->
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.5.3.201107060350</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
            <configuration>
              <!-- Where to put jacoco coverage report -->
              <destFile>${sonar.jacoco.reportPath}</destFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>



C: run mvn clean install -PcodeCoverage   it will generate jacoco.exec file in your given directory. This file later can be used for sonar analysis.