Skip to content
Elliott Pope edited this page Feb 9, 2021 · 88 revisions

rest-driver

Test Driver to test your RESTful services and clients

There are two libraries here:

  • REST server driver - for testing your RESTful service
  • REST client driver - for testing your RESTful client & mocking remote services

Latest version: 2.0.1 (29 April 2016)

Downloads - Server Driver - Client Driver

Goals

Everyone knows that testing is good for your health and REST is good for your sanity. Now it is easy to keep both in check by allowing you to write quick, readable, fluid tests which are easy to refactor and give excellent error handling/reporting. So we provide these libraries to test from both ends of the pipe.

REST server driver

In order to do thorough testing of your RESTful service, you'll have to make actual HTTP requests and check the actual HTTP responses from your service. REST driver makes this as easy as:

import static com.github.restdriver.serverdriver.Matchers.*;
import static com.github.restdriver.serverdriver.RestServerDriver.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import org.junit.Test;

import com.github.restdriver.serverdriver.http.response.Response;

public class MyTest {
    @Test
    public void testExample() {
        Response response = get( "http://www.example.com" );
        assertThat(response, hasStatusCode(200));
        assertThat(response.asJson(), hasJsonPath("$.name", equalTo("jeff")));
    }
}

More info: Server Driver

REST client driver

If you have a client for a RESTful service, it's not ideal to have an actual service running somewhere to test against. This is difficult to keep on top of, makes for brittle/flickering tests, and tightly couples your tests to someone else's code.

We provide a mock-like interface which launches a real HTTP server and allows setup like this:

import static com.github.restdriver.clientdriver.RestClientDriver.*;

import org.junit.Rule;
import org.junit.Test;

import com.github.restdriver.clientdriver.ClientDriverRequest.Method;
import com.github.restdriver.clientdriver.ClientDriverRule;

public class MyTest {
    @Rule
    public ClientDriverRule clientDriver = new ClientDriverRule(1234);
    
    @Test
    public void testRoot() {
        clientDriver.addExpectation(
                onRequestTo("/").withMethod(Method.GET), 
                giveResponse("some wonderful content", "text/plain"));
    }
}

The server will listen for a requests and respond with the replies you set. Any unexpected action or unmet expectation will fail your tests.

You can also use the client-driver to mock out any remote services which your RESTful service uses.

More info: Client Driver

Clone this wiki locally