Skip to content

Zigu/hoverfly-java

 
 

Repository files navigation

Hoverfly Java - Easy creation of stub http servers for testing

CircleCI Codecov Maven Central

A Java native language binding for Hoverfly, a Go proxy which allows you to simulate http services in your unit tests. Another term for this is Service Virtualisation.

Features

  • Simulation of http services

  • Strict or loose http request matching based on URL, method, body and header combinations

  • Fluent and expressive DSL for easy generation of simulated services

  • Automatic marshalling of objects into JSON during request / response body generation

  • Create simulations by running in capture mode

  • Hoverfly is a proxy, so you don’t need to alter the host that you make requests to

  • Multiple hosts / services per single instance of Hoverfly

  • Https automatically supported, no extra configuration required

  • Interoperable with standard Hoverfly json, making it easy to re-use data between Java and other bindings.

Documentation

Full documentation is available here

Maven Dependency

<dependency>
    <groupId>io.specto</groupId>
    <artifactId>hoverfly-java</artifactId>
    <version>0.3.6</version>
    <scope>test</scope>
</dependency>

Example

Create API simulation using capture mode

// Capture and output HTTP traffic to json file
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inCaptureMode("simulation.json");


// After the capturing, switch to inSimulationMode to spin up a stub server
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(classpath("simulation.json"));

// Or you can use both approaches at once. If json file not present in capture mode, if present in simulation mode
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inCaptureOrSimulationMode("simulation.json");

Create API simulation using DSL

@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulateMode(dsl(
    service("www.my-test.com")
        .get("/api/bookings/1")
        .willReturn(success(json(new Booking(1))))
));

@Test
public void shouldBeAbleToGetABookingUsingHoverfly() {
    // When
    final ResponseEntity<String> getBookingResponse = restTemplate.getForEntity("http://www.my-test.com/api/bookings/1", String.class);

    // Then
    assertThat(getBookingResponse.getStatusCode()).isEqualTo(OK);
    assertThatJSON(getBookingResponse.getBody()).isEqualTo("{"\"bookingId\":\"1\"}");
}

Contributions

Contributions are welcome!

To submit a pull request you should fork the Hoverfly-Java repository, and make your change on a feature branch of your fork.

Issues

Feel free to raise an issues on Github.

License

Apache License version 2.0.

(c) SpectoLabs 2016.

About

Java library for Hoverfly

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE.txt
Unknown
license-header.txt

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.4%
  • Shell 0.6%