Quickstart¶
Maven¶
If using Maven, add the following dependency to your pom:
<dependency> <groupId>io.specto</groupId> <artifactId>hoverfly-java</artifactId> <version>0.12.2</version> <scope>test</scope> </dependency>
Gradle¶
Or with Gradle add the dependency to your *.gradle file:
testCompile "io.specto:hoverfly-java:0.12.2"
Code example¶
The simplest way is to get started is with the JUnit rule. Behind the scenes the JVM proxy settings will be configured to use the managed Hoverfly process, so you can just make requests as normal, only this time Hoverfly will respond instead of the real service (assuming your HTTP client respects JVM proxy settings):
import static io.specto.hoverfly.junit.core.SimulationSource.dsl;
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service;
import static io.specto.hoverfly.junit.dsl.ResponseCreators.success;
public class HoverflyExample {
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(dsl(
service("www.my-test.com")
.get("/api/bookings/1")
.willReturn(success("{\"bookingId\":\"1\"}", "application/json"))
));
@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\"}");
}
// Continues...