Skip to content

Using the Alternative Stub Server Implementation

Jan Dudek edited this page Mar 25, 2016 · 11 revisions

A stub http server is a mandatory Jadler component which accepts all incoming http requests during the integration test execution and produces stub http responses. This component is pluggable (and therefore interchangeable), the implementation is required to implement the StubHttpServer interface and stick to the contract described in it.

Jadler provides a standard implementation of the stub server built using the Jetty http server. Although it's the standard and recommended way to use Jadler, there may be special circumstances that the Jetty library may cause troubles. More specifically, in some specific setup you may encounter version clashes of the servlet implementations (the implementation used by Jetty vs an implementation used in the tested application).

If you encounter such a problem you can use an alternative implementation of the stub http server provided by Jadler. This implementation uses a rather non-standard com.sun.net.httpserver package. Even though it's not a standart part of a JRE it seems to be available on both Oracle and OpenJDK implementations.

So if you come to a conclusion that the alternative stub server implementation might be handy in your testing environment, just follow the following steps.

Configuring Your Project

To use Jadler with the alternative stub server implementation you have to specify the Jadler artifacts explicitly instead of using the jadler-all package (more on the topic):

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

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

</dependencies>

Setting Jadler to Use the Alternative Server

Once the artifacts are available on the classpath all you have to do is configuring Jadler in the following way:

import net.jadler.stubbing.server.jdk.JdkStubHttpServer;
import static net.jadler.Jadler.initJadlerUsing;
import static net.jadler.Jadler.closeJadler;

...

@Before
public void setUp() {
    initJadlerUsing(new JdkStubHttpServer());
}

@After
public void tearDown() {
    closeJadler();
}

You can also use the jUnit Jadler rule instead of the setUp/tearDown methods in jUnit:

import org.junit.Rule;
import net.jadler.junit.rule.JadlerRule;
import net.jadler.stubbing.server.jdk.JdkStubHttpServer;

...

@Rule
public final JadlerRule rule = new JadlerRule(new JdkStubHttpServer());
...

And that's it, Jadler will use the alternative stub server for all tests in this suite.