How to get unit tests to run in Maven Tycho build?
Asked Answered
M

2

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?

Milson answered 5/4, 2016 at 16:0 Comment(0)
T
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.

Thimbu answered 5/7, 2017 at 12:42 Comment(4)
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?Impressionable
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?Impressionable
For JUnit 5 and/or mixed Junit 4 and 5: wiki.eclipse.org/Tycho/How_Tos/JUnit5Dale
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 helpfulSubserve
M
-1

You have to use junit and Mockito as osgi bundles

I think this Question answered detailed here

I hope this helps.

Moulmein answered 22/4, 2016 at 11:5 Comment(1)
Actually no. You dont "have to" go through this hell. It's just another, more complicated option. See my answerThimbu

© 2022 - 2024 — McMap. All rights reserved.