Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get unit tests to run in Maven Tycho build?

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?

like image 721
David M. Karr Avatar asked Apr 05 '16 16:04

David M. Karr


People also ask

What do we used to run unit test with Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.


1 Answers

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.

like image 192
Alex Avatar answered Sep 19 '22 08:09

Alex