Skip to content

Adding Jadler

Jan Dudek edited this page Mar 22, 2016 · 46 revisions

Adding Jadler to Your Project

The easiest way to add Jadler to a Maven based project is putting the jadler-all dependency to the pom.xml file:

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-all</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

The jadler-all artifact adds all necessary Jadler components (jadler-core, jadler-jetty and jadler-junit) automatically.

Fine Tuning the Jadler Inclusion

Instead of adding all Jadler components automatically to your project you can just specify the artifacts you really need. Jadler consists of following modules:

  • jadler-core - base Jadler module, mandatory for Jadler usage
  • jadler-jetty - Jetty based implementation of an http stub server. This dependency is necessary if the default Jetty based stub server is intended to be used.
  • jadler-junit - jUnit specific support
  • jadler-jdk - an alternative implementation of the stub server using the com.sun.net.httpserver package.

For example, if you want to use the standard Jetty stub server implementation and no jUnit support is needed, you can just put these two modules to the pom.xml file:

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-core</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-jetty</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

And vice versa, if the alternative httpserver implementation will be used as well as the jUnit support, you can just use the following configuration:

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-core</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-jdk</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-junit</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

</dependencies>

Jadler & JUnit

It comes as no surprise that Jadler will be very often used in conjunction with JUnit. However there's a catch when using JUnit prior the 4.11 version. It has few Hamcrest classes embedded (inlined) coming from the old 1.1 version while Jadler requires the newest 1.3 version.

To include JUnit prior 4.11 to your Maven project use the junit-dep artifact instead of the more common junit:

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-all</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit-dep</artifactId>
        <version>4.10</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

The exclusion ensures Maven doesn't resolve the hamcrest-core dependency in favor of the old 1.1 version. Instead of an exclusion one can use a dependency management section:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-all</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit-dep</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
</dependencies>

The dependency section explicitly enforces a specific version of a library. More on the topic can be found in What's up with the JUnit and Hamcrest Dependencies?.

Jadler & Mockito

Mockito is another library Jadler will be very often used along with. And there is a very similar catch as described in the JUnit section.

The mockito-all artifact has few Hamcrest classes (in 1.1 version) embedded so it's necessary to use mockito-core instead. And since mockito-core depends on hamcrest-core version 1.1, it must be excluded in the exactly same way as already described before:

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-all</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

A dependency-management section can be used of course as well:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>net.jadler</groupId>
        <artifactId>jadler-all</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Conclusion

When using Jadler in your project it's always good to check which version Hamcrest (specifically hamcrest-core) was resolved to. This can be done using mvn dependency:tree:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jadler-dependency-example 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] net.jadler:jadler-dependency-example:jar:1.0.0-SNAPSHOT
[INFO] +- net.jadler:jadler-all:jar:1.3.0:compile
[INFO] |  +- net.jadler:jadler-core:jar:1.3.0:compile
[INFO] |  |  +- org.slf4j:slf4j-api:jar:1.7.2:compile
[INFO] |  |  +- org.hamcrest:hamcrest-library:jar:1.3:compile
[INFO] |  |  +- commons-io:commons-io:jar:2.4:compile
[INFO] |  |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  |  \- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  \- net.jadler:jadler-jetty:jar:1.3.0:compile
[INFO] |     +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] |     \- org.eclipse.jetty:jetty-server:jar:7.1.6.v20100715:compile
[INFO] |        +- org.eclipse.jetty:jetty-continuation:jar:7.1.6.v20100715:compile
[INFO] |        \- org.eclipse.jetty:jetty-http:jar:7.1.6.v20100715:compile
[INFO] |           \- org.eclipse.jetty:jetty-io:jar:7.1.6.v20100715:compile
[INFO] |              \- org.eclipse.jetty:jetty-util:jar:7.1.6.v20100715:compile
[INFO] +- org.mockito:mockito-core:jar:1.9.5:compile
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] |  \- org.objenesis:objenesis:jar:1.0:compile
[INFO] \- junit:junit-dep:jar:4.10:compile

You can’t perform that action at this time.