Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which dependencies do I need to use Mockito and JUnit in an Eclipse RCP Tycho project

This is my current test fragment:

<packaging>eclipse-test-plugin</packaging>

<dependencies>
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>com.springsource.org.junit</artifactId>
        <version>4.7.0</version>
    </dependency>
</dependencies>

with the following plugins configuration:

<plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>tycho-surefire-plugin</artifactId>
    <version>${tycho.version}</version>
    <configuration>
        <dependencies>
            <dependency>
                <type>p2-installable-unit</type>
                <artifactId>org.eclipse.equinox.ds</artifactId>
            </dependency>
            <dependency>
                <type>p2-installable-unit</type>
                <artifactId>org.apache.felix.gogo.shell</artifactId>
            </dependency>
        </dependencies>
        <providerHint>junit47</providerHint>
        <argLine>-ea</argLine>
    </configuration>
</plugin>

and I use the POM-first approach to resolve dependencies:

<pomDependencies>consider</pomDependencies>

The above JUnit Version is the only one I could find, that is packaged as a bundle.

The problem is that I cannot find a match which allows me to use JUnit and Mockito together in a fragment.

My common issues are:

  • Mockito-core from Maven Central needs Hamcrest 1.0-2.0, but the JUnit bundle exports Hamcrest in version 4.7.0
  • There is no junit-dep bundle available in the Springsource repository
  • When I add another Hamcrest bundle, I have version conflicts between the versions exported by JUnit (4.7.0) and the Hamcrest bundle (1.3)

I would like to avoid creating my own bundle from JUnit, Hamcrest and Mockito.

like image 472
oers Avatar asked Aug 08 '13 09:08

oers


1 Answers

I have found that the wrapper bundles of JUnit, Hamcrest, and Mockito from the Eclipse Orbit work well together.

For the (currently) latest Orbit release, which includes JUnit 4.11, Hamcrest 1.1 (with Hamcrest Core in version 1.3), and Mockito 1.8.4, just add the following snippet to your POM:

<repositories>
    <repository>
        <id>orbit-kepler</id>
        <url>http://download.eclipse.org/tools/orbit/downloads/drops/R20130517111416/repository/</url>
        <layout>p2</layout>
    </repository>
</repositories>

In the wrappers of the Eclipse Orbit, the org.junit bundle exports parts of the package org.hamcrest.core. Mockito however needs the complete content of the org.hamcrest.core package. In order to prevent accidental wiring between the Mockito and JUnit bundle, the export is marked with a mandatory attribute. Unfortunately, p2 doesn't take these into account (and Tycho uses p2 for dependency resolution), so you need to give the dependency resolution of your fragment (using Mockito) an extra hint:

<plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>target-platform-configuration</artifactId>
    <version>${tycho-version}</version>
    <configuration>
        <dependency-resolution>
            <extraRequirements>
                <requirement>
                    <type>eclipse-plugin</type>
                    <id>org.hamcrest</id>
                    <versionRange>0.0.0</versionRange>
                </requirement>
            </extraRequirements>
        </dependency-resolution>
    </configuration>
</plugin>

This makes sure that the org.hamcrest bundle is used during dependency resolution, and that Mokito's imports can be wired successfully.

like image 146
oberlies Avatar answered Oct 04 '22 02:10

oberlies