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

1

11

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.

Johnnajohnnie answered 8/8, 2013 at 9:47 Comment(1)
Tycho is never POM-first, which would mean that the POM is the leading source for dependency declarations. In Tycho, the dependencies are always resolved from the manifest. With pomDependencies=consider you just activate a mechanism to add bundles from Maven repositories to the target platform. (Typically, these bundles are built with a POM-first approach, i.e. with the maven-bundle-plugin.)Spaceport
S
18

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.

Spaceport answered 8/8, 2013 at 12:0 Comment(7)
could you please elaborate where and how I would declare those dependencies. If I declare them normally in my master.pom, maven complains that it needs a groupId (which I cannot find). And I might add, that I use a target-definition to make tycho and eclipse work together.Johnnajohnnie
Let's try to make the Tycho build work first: Just declare package imports/bundle requires in your manifest, and Tycho will pick the bundles from the Orbit repository. (How to get Mockito/Hamcrest/JUnit into a target file would make a good separate question.)Spaceport
this just blew my mind :) What I can see now, is that tycho downloads the artifacts from the newly added repository (while eclipse is still complaining about missing stuff). And the Test is running.Johnnajohnnie
I've added the orbit repository to my target-definition file and now eclipse picks this up (after restarting eclipse, switching the target platform back and force, etc.. ;)Johnnajohnnie
@Spaceport I wish I could give you more upvotes! Do you know if there is a similar update site for org.assertJ fluet assertions? Thanks Again!Isoniazid
@SheldonWarkentin: I don't. But you'll reach a wider audience if you ask this as a new questions. Follow-up questions are discouraged on stackoverflow - but only because they don't work well.Spaceport
Your <dependency-resolution> save my day! Thanks!Menticide

© 2022 - 2024 — McMap. All rights reserved.