Skip to content
jandudek edited this page Oct 7, 2015 · 71 revisions

Build Status

Welcome to Jadler, Java http mocking library.

If you need a simple way to create a stub http server in your integration tests and do some http communication mocking, look no further! Writing tests for http client applications can be as easy as:

@Test
public void getAccount() {

    onRequest()
        .havingMethodEqualTo("GET")
        .havingURIEqualTo("/accounts/1")
        .havingBody(isEmptyOrNullString())
        .havingHeaderEqualTo("Accept", "application/json")
    .respond()
        .withTimeout(2, SECONDS)
        .withStatus(200)
        .withBody("{\\"account\\":{\\"id\\" : 1}}")
        .withEncoding(Charset.forName("UTF-8"))
        .withContentType("application/json; charset=UTF-8");

    final AccountService service = new AccountServiceRestImpl("http", "localhost", port());
    final Account account = service.getAccount(1);
       
    assertThat(account, is(notNullValue()));
    assertThat(account.getId(), is(1));
}


@Test
public void deleteAccount() {

    onRequest()
        .havingMethodEqualTo("DELETE")
        .havingPathEqualTo("/accounts/1")
    .respond()
        .withStatus(204);

    final AccountService service = new AccountServiceRestImpl("http", "localhost", port());
    service.deleteAccount(1);
     
    verifyThatRequest()
        .havingMethodEqualTo("DELETE")
        .havingPathEqualTo("/accounts/1")
    .receivedOnce();
}

@Test
public void getFile() throws IOException{
    initJadlerListeningOn(8081).that().respondsWithDefaultStatus(200);
    String fileService = "http://localhost:8081/getExcelFile";

    byte[] bytes = IOUtils.toByteArray(new FileInputStream(myExcelFile));
        
    onRequest()
        .havingPathEqualTo("/getExcelFile")
    .respond()
        .withBody(bytes)
	.withContentType("application/vnd.ms-excel")
	.withHeader("Content-Length", Integer.toString(bytes.length));
	     
    HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
    GetMethod get = new GetMethod(new URL(fileService).toExternalForm());
    int responseCode = client.executeMethod(get);
	     
    assertEquals(200, responseCode);
    assertEquals(bytes.length, get.getResponseBodyAsString().getBytes().length);
}

What's new

  • don't forget to follow Jadler on Twitter to learn easily about new releases and features: @JadlerMocker
  • Changelog

2015-10-05

New minor version 1.1.2 is now available. Thanks to lukas-krecan and rosell for their contribution and bug reports! Read more about the release.

2014-06-07

New minor version 1.1.1 is now available thanks to a contribution from christiangalsterer!

2013-11-02

Jadler 1.1.0 is out! Special thanks to benky and lukas-krecan for their contributions and reviews!

2013-07-01

Jadler 1.0.0 released! The first stable version is finally out! Special thanks to benky, liry and lukas-krecan for their contributions and reviews!

2013-03-18

Jadler 0.9.5 released! Special thanks to benky, liry and jumarko for their contributions!

Adding Jadler

To add Jadler to your Java project just put the following dependency to the Maven pom.xml file:

<dependency>
    <groupId>net.jadler</groupId>
    <artifactId>jadler-all</artifactId>
    <version>1.1.2</version>
    <scope>test</scope>
</dependency>
Clone this wiki locally