10

I've done a lot of work in the past writing unit tests that run in "conventional" Maven builds, using JUnit and Mockito (and PowerMock). I'm now working on an Eclipse plugin codebase, which builds with Maven Tycho.

Overall, it's a multiproject build, but I'm only adding unit tests to one of the plugin projects (for now).

I've heard of tycho-surefire, but that seems pretty complicated, and it really sounds more like it supports integration tests instead of unit tests. I'm guessing I'll probably have no choice but to use this, but so far I haven't tried to integrate it.

I tried getting the JUnit and Mockito artifacts from Maven, and then using the maven-dependency-plugin to get the artifacts available to be referenced in the Bundle-Classpath property of the manifest.

When I run the build, the tycho-compiler-plugin I see it compiling 105 source files, which includes all of the classes in src/main/java and src/test/java. It fails to compile the test class because it can't find the Mockito classes, even though when I run the build with -X, it shows the mockito-all artifact in the dependency tree.

What can I do here?

Lii
  • 11,553
  • 8
  • 64
  • 88
David M. Karr
  • 14,317
  • 20
  • 94
  • 199

2 Answers2

5

After a lot of painful Maven trial & error I struggled across this website, which provides a surprisingly easy way to use unit-tests in a Maven-Tycho setup.

Here, the important parts of the pom.xml when using JUnit (probably looks similar for Mockito):

<testSourceDirectory>src/test/java</testSourceDirectory>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <executions>
      <execution>
        <id>test</id>
        <phase>test</phase>
        <configuration>
          <includes>
            <include>**/*Test.java</include>
          </includes>
        </configuration>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
        <id>compiletests</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

Name all your tests in a way so that they end with *Test.java. Run mvn test in order to execute all available unit-tests.

Lii
  • 11,553
  • 8
  • 64
  • 88
Alex
  • 1,602
  • 20
  • 33
  • 1
    Hello Alex! Is this configuration supposed to go in a Maven module with Tycho's `eclipse-test-plugin` packaging? On in a non-Tycho maven module? – Lii May 29 '18 at 08:45
  • 1
    Also, can the tests in this configuration use dependencies from the Eclipse plugin `MANIFEST.MF` file? Or do you have to declare all dependencies for you test code in your Maven pom file? – Lii May 29 '18 at 08:46
  • For JUnit 5 and/or mixed Junit 4 and 5: https://wiki.eclipse.org/Tycho/How_Tos/JUnit5 – Max Hohenegger Jun 27 '18 at 14:39
  • It's a bit absurd that I need to consult a 9+ year old blog to find a working guide to something so fundamental as unit testing... Thanks anyway, very helpful – jnnks Nov 23 '21 at 21:07
-1

You have to use junit and Mockito as osgi bundles

I think this Question answered detailed here

I hope this helps.

Community
  • 1
  • 1
Hisham Khalil
  • 1,054
  • 8
  • 9
  • Actually no. You dont "have to" go through this hell. It's just another, more complicated option. See my answer – Alex Jul 05 '17 at 12:44