Fork me on GitHub

Java Stub Server is an embedded HTTP server, started up by your tests, set up with expectations like a mock, and called by your application under test.

Using embedded Jetty to spin up a server, with Mockito-esque expectations to match URLs, verbs and headers, then return the expected response.

Capture the request so you can assert on the content, or just verify the call was made.


Dependency

The library is available from Maven Central, and can be included in your projects dependencies

    <dependency>
        <groupId>com.pyruby</groupId>
        <artifactId>java-stub-server</artifactId>
        <version>0.12</version>
    </dependency>

Example

As the library was written Test First, the repository is full of useful tests which serve as the most comprehensive documentation. Here are a few of the basics.

Expect a GET request

    @Test
    public void myApplication_shouldGetTheCustomerFromTheCustomerReSTApi() {
        Server server = new Server(44041);
        server.start();
        try {
            server.expect(StubMethod.get("/customers/123")).thenReturn(200, "application/json", " ... ");

            // call your application which should invoke the customer ReST api on port 44041
            ...

            server.verify();
        } finally {
            server.stop();
        }
    }
            

This starts an HTTP server on port 44041, expects a GET request to http://localhost:44041/customers/123 and when it receives it, responds with the supplied application/json responce, with a 200 response code.

Check a PUT sends the correct payload

    @Test
    public void myApplication_shouldPutTheCustomerToTheCustomerReSTApi() {
        Server server = new Server(44041);
        server.start();
        try {
            StubMethod expectedCall = StubMethod.put("/customers/\\d")
                .ifContentType("application/json")
                .ifHeader("someHeader", "expectedValue");
            server.expect(expectedCall).thenReturn(204);

            // call your application which should invoke the customer ReST api on port 44041
            ...

            server.verify();
            assertEquals(" expected customer ", expectedCall.bodyString());
        } finally {
            server.stop();
        }
    }
            

This time, the expected call is a PUT, that expects a url http://localhost:44041/customers/{customerNumericId}. The PUT must have a Content-Type of application/json with a header someHeader: expectedValue

Once called, the body that was passed by your application can be obtained and asserted against, either as raw bytes, or in this case as a String.

HTTPS

    @Test
    public void myApplication_shouldCallHTTPSReSTApi() {
        HttpsSettings httpsSettings = new HttpsSettings(keystorePath, keystorePassword, keyPassword);
        Server server = new Server(22043, httpsSettings);
        server.start();
        try {
            // all the same expectations etc
            ...
        } finally {
            server.stop();
        }
    }
            

The same assertions are available over SSL, by using the HttpsSettings parameter of the Server constructor.


License

© 2011-2013 - Chris Tarttelin for PyRuby LTD

Released under the FreeBSD licence, as defined here http://www.freebsd.org/copyright/freebsd-license.html.