public class HoverflyRule
extends org.junit.rules.ExternalResource
The HoverflyRule auto-spins up a Hoverfly process, and tears it down at the end of your tests. It also configures the JVM
proxy to use Hoverfly, so so long as your client respects these proxy settings you shouldn't have to configure it.
public class SomeTest {
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(classpath("test-service.json"))
@Test
public void test() { //All requests will be proxied through Hoverfly
// Given
final RequestEntity<Void> bookFlightRequest = RequestEntity.delete(new URI("http://www.other-anotherService.com/api/bookings/1")).build();
// When
final ResponseEntity<Void> bookFlightResponse = restTemplate.exchange(bookFlightRequest, Void.class);
// Then
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
}
}
You can provide data from a Hoverfly JSON simulation, or alternatively you can use a DSL - HoverflyDsl
It is also possible to capture data:
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inCaptureMode("recorded-simulation.json");
The recorded data will be saved in your src/test/resources directory
It's recommended to always use the ClassRule annotation, so you can share the same instance of Hoverfly through all your tests.
This avoids the overhead of starting Hoverfly multiple times, and also helps ensure all your system properties are set before executing any other code.
If you want to change the data, you can do so in Before method by calling simulate(io.specto.hoverfly.junit.core.SimulationSource), but this will not be thread safe.
SimulationSource,
HoverflyDsl| Modifier and Type | Method and Description |
|---|---|
protected void |
after()
Stops the managed instance of Hoverfly
|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
Log warning if
HoverflyRule is annotated with Rule |
protected void |
before()
Starts in instance of Hoverfly
|
HoverflyMode |
getHoverflyMode()
Gets started Hoverfly mode
|
int |
getProxyPort()
Gets the proxy port this has run on, which could be useful when running
Hoverfly on a random port. |
static HoverflyRule |
inCaptureMode(java.lang.String outputFilename)
Instantiates a rule which runs
Hoverfly in capture mode |
static HoverflyRule |
inCaptureMode(java.lang.String outputFilename,
HoverflyConfig hoverflyConfig)
Instantiates a rule which runs
Hoverfly in capture mode |
static HoverflyRule |
inCaptureOrSimulationMode(java.lang.String recordFile)
Instantiates a rule which runs
Hoverfly in capture mode if
recorded file is not present, or in simulation mode if record file is present |
static HoverflyRule |
inCaptureOrSimulationMode(java.lang.String recordFile,
HoverflyConfig hoverflyConfig)
Instantiates a rule which runs
Hoverfly in capture mode if
recorded file is not present, or in simulation mode if record file is present |
static HoverflyRule |
inSimulationMode()
Instantiates a rule which runs
Hoverfly in simulate mode with no data |
static HoverflyRule |
inSimulationMode(HoverflyConfig hoverflyConfig)
Instantiates a rule which runs
Hoverfly in simulate mode with no data |
static HoverflyRule |
inSimulationMode(SimulationSource simulationSource)
Instantiates a rule which runs
Hoverfly in simulate mode |
static HoverflyRule |
inSimulationMode(SimulationSource simulationSource,
HoverflyConfig hoverflyConfig) |
void |
simulate(SimulationSource simulationSource)
Changes the Simulation used by
Hoverfly |
public static HoverflyRule inCaptureOrSimulationMode(java.lang.String recordFile)
Hoverfly in capture mode if
recorded file is not present, or in simulation mode if record file is presentrecordFile - the path where captured or simulated traffic is taken. Relative to src/test/resourcespublic static HoverflyRule inCaptureOrSimulationMode(java.lang.String recordFile, HoverflyConfig hoverflyConfig)
Hoverfly in capture mode if
recorded file is not present, or in simulation mode if record file is presentrecordFile - the path where captured or simulated traffic is taken. Relative to src/test/resourceshoverflyConfig - the configpublic static HoverflyRule inCaptureMode(java.lang.String outputFilename)
Hoverfly in capture modeoutputFilename - the path to the recorded name relative to src/test/resourcespublic static HoverflyRule inCaptureMode(java.lang.String outputFilename, HoverflyConfig hoverflyConfig)
Hoverfly in capture modeoutputFilename - the path to the recorded name relative to src/test/resourceshoverflyConfig - the configpublic static HoverflyRule inSimulationMode(SimulationSource simulationSource)
Hoverfly in simulate modesimulationSource - the simulation to importpublic static HoverflyRule inSimulationMode(SimulationSource simulationSource, HoverflyConfig hoverflyConfig)
public static HoverflyRule inSimulationMode()
Hoverfly in simulate mode with no datapublic static HoverflyRule inSimulationMode(HoverflyConfig hoverflyConfig)
Hoverfly in simulate mode with no datahoverflyConfig - the configpublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
HoverflyRule is annotated with Ruleapply in interface org.junit.rules.TestRuleapply in class org.junit.rules.ExternalResourceprotected void before()
throws java.lang.Throwable
before in class org.junit.rules.ExternalResourcejava.lang.Throwableprotected void after()
after in class org.junit.rules.ExternalResourcepublic int getProxyPort()
Hoverfly on a random port.public HoverflyMode getHoverflyMode()
public void simulate(SimulationSource simulationSource)
HoverflysimulationSource - the simulation