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/hoverfly 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, 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 |
void |
assertThatNoDiffIsReported()
Asserts that there was no diff between any of the expected responses set by simulations and the actual responses
returned from the real service.
|
void |
assertThatNoDiffIsReported(boolean shouldResetDiff)
Asserts that there was no diff between any of the expected responses set by simulations and the actual responses
returned from the real service.
|
protected void |
before()
Starts an instance of Hoverfly
|
void |
capture(java.lang.String outputFilename)
Stores what's currently been captured in the currently assigned file, reset simulations and journal logs, then starts capture again
ready to store in the new file once complete.
|
void |
capture(java.lang.String outputDir,
java.lang.String outputFilename)
Stores what's currently been captured in the currently assigned file, reset simulations and journal logs, then starts capture again
ready to store in the new file once complete.
|
java.lang.String |
getAuthHeaderName()
Deprecated.
|
java.lang.String |
getAuthHeaderValue()
Deprecated.
|
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. |
SslConfigurer |
getSslConfigurer() |
java.util.Map<java.lang.String,java.lang.String> |
getState()
Get all state from Hoverfly
|
static HoverflyRule |
inCaptureMode() |
static HoverflyRule |
inCaptureMode(HoverflyConfig hoverflyConfig) |
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 |
inCaptureMode(java.lang.String outputDir,
java.lang.String outputFilename)
Instantiates a rule which runs
Hoverfly in capture mode |
static HoverflyRule |
inCaptureMode(java.lang.String outputDir,
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 |
inDiffMode()
Instantiates a rule which runs
Hoverfly in diff mode with no data |
static HoverflyRule |
inDiffMode(HoverflyConfig hoverflyConfig)
Instantiates a rule which runs
Hoverfly in diff mode with no data |
static HoverflyRule |
inDiffMode(SimulationSource simulationSource)
Instantiates a rule which runs
Hoverfly in diff mode |
static HoverflyRule |
inDiffMode(SimulationSource simulationSource,
HoverflyConfig hoverflyConfig)
Instantiates a rule which runs
Hoverfly in diff mode |
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) |
static HoverflyRule |
inSpyMode()
Instantiates a rule which runs
Hoverfly in spy mode with no data |
static HoverflyRule |
inSpyMode(HoverflyConfig hoverflyConfig)
Instantiates a rule which runs
Hoverfly in spy mode with no data |
static HoverflyRule |
inSpyMode(SimulationSource simulationSource)
Instantiates a rule which runs
Hoverfly in spy mode |
static HoverflyRule |
inSpyMode(SimulationSource simulationSource,
HoverflyConfig hoverflyConfig) |
HoverflyRule |
printSimulationData()
Print the simulation data to console for debugging purpose.
|
void |
resetDiffs() |
void |
resetJournal() |
void |
resetState()
Deletes all state from Hoverfly
|
void |
setState(java.util.Map<java.lang.String,java.lang.String> state)
Deletes all state from Hoverfly and then sets the state.
|
void |
simulate(SimulationSource simulationSource,
SimulationSource... sources)
Changes the Simulation used by
Hoverfly
It also reset the journal to ensure verification can be done on the new simulation source. |
void |
updateState(java.util.Map<java.lang.String,java.lang.String> state)
Updates state in Hoverfly.
|
void |
verify(RequestMatcherBuilder requestMatcher) |
void |
verify(RequestMatcherBuilder requestMatcher,
VerificationCriteria criteria) |
void |
verifyAll() |
void |
verifyZeroRequestTo(StubServiceBuilder requestedServiceBuilder) |
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/resources/hoverflypublic 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/resources/hoverflyhoverflyConfig - the configpublic static HoverflyRule inCaptureMode()
public static HoverflyRule inCaptureMode(HoverflyConfig hoverflyConfig)
public static HoverflyRule inCaptureMode(java.lang.String outputFilename)
Hoverfly in capture modeoutputFilename - the output simulation file name relative to src/test/resources/hoverflypublic static HoverflyRule inCaptureMode(java.lang.String outputFilename, HoverflyConfig hoverflyConfig)
Hoverfly in capture modeoutputFilename - the output simulation file name relative to src/test/resources/hoverflyhoverflyConfig - the configpublic static HoverflyRule inCaptureMode(java.lang.String outputDir, java.lang.String outputFilename)
Hoverfly in capture modeoutputDir - the directory path relative to your project root for exporting the simulation fileoutputFilename - the output simulation file namepublic static HoverflyRule inCaptureMode(java.lang.String outputDir, java.lang.String outputFilename, HoverflyConfig hoverflyConfig)
Hoverfly in capture modeoutputDir - the directory path relative to your project root for exporting the simulation fileoutputFilename - the output simulation file namehoverflyConfig - the configpublic static HoverflyRule inSimulationMode()
Hoverfly in simulate mode with no datapublic static HoverflyRule inSimulationMode(HoverflyConfig hoverflyConfig)
Hoverfly in simulate mode with no datahoverflyConfig - 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 inSpyMode()
Hoverfly in spy mode with no datapublic static HoverflyRule inSpyMode(HoverflyConfig hoverflyConfig)
Hoverfly in spy mode with no datahoverflyConfig - the configpublic static HoverflyRule inSpyMode(SimulationSource simulationSource)
Hoverfly in spy modesimulationSource - the simulation to importpublic static HoverflyRule inSpyMode(SimulationSource simulationSource, HoverflyConfig hoverflyConfig)
public static HoverflyRule inDiffMode()
Hoverfly in diff mode with no datapublic static HoverflyRule inDiffMode(HoverflyConfig hoverflyConfig)
Hoverfly in diff mode with no datahoverflyConfig - the configpublic static HoverflyRule inDiffMode(SimulationSource simulationSource)
Hoverfly in diff modesimulationSource - the simulation to import the responses will be compared topublic static HoverflyRule inDiffMode(SimulationSource simulationSource, HoverflyConfig hoverflyConfig)
Hoverfly in diff modesimulationSource - the simulation to import the responses will be compared tohoverflyConfig - 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()
before in class org.junit.rules.ExternalResourceprotected void after()
after in class org.junit.rules.ExternalResourcepublic int getProxyPort()
Hoverfly on a random port.public SslConfigurer getSslConfigurer()
public HoverflyMode getHoverflyMode()
public void simulate(SimulationSource simulationSource, SimulationSource... sources)
Hoverfly
It also reset the journal to ensure verification can be done on the new simulation source.simulationSource - the simulationpublic void capture(java.lang.String outputFilename)
outputFilename - the output simulation file name relative to src/test/resources/hoverflypublic void capture(java.lang.String outputDir,
java.lang.String outputFilename)
outputDir - the directory path relative to your project root for exporting the simulation fileoutputFilename - the output simulation file name relative to src/test/resources/hoverfly@Deprecated public java.lang.String getAuthHeaderName()
@Deprecated public java.lang.String getAuthHeaderValue()
public HoverflyRule printSimulationData()
public void verify(RequestMatcherBuilder requestMatcher)
public void verify(RequestMatcherBuilder requestMatcher, VerificationCriteria criteria)
public void verifyZeroRequestTo(StubServiceBuilder requestedServiceBuilder)
public void verifyAll()
public void resetJournal()
public void resetState()
public java.util.Map<java.lang.String,java.lang.String> getState()
public void setState(java.util.Map<java.lang.String,java.lang.String> state)
state - the new statepublic void updateState(java.util.Map<java.lang.String,java.lang.String> state)
state - the state to update withpublic void resetDiffs()
public void assertThatNoDiffIsReported()
public void assertThatNoDiffIsReported(boolean shouldResetDiff)
shouldResetDiff says if all available diffs should be removed when the assertion is done.shouldResetDiff - if all available diffs should be removed when the assertion is done.