hoverfly-java
hoverfly-java copied to clipboard
HoverflyRule doesn't export simulations for multiple tests
When HoverflyRule is annotated with @Rule, the exportSimulation is called after each test, meaning that, it will keep overwriting old captured data because of the same filename.
The solution could be postfixing the file name with the test class names, so that
@Rule
public HoverflyRule hoverflyRule = HoverflyRule.inCaptureMode("booking_service_test.json")
@Test
public shouldGetBooking() {
}
@Test
public shouldDeleteBooking() {
}
should create two files under test/resources/hoverfly
booking_service_test_shouldGetBooking.jsonbooking_service_test_shouldDeleteBooking.json
When I change inCaptureMode("some-test.json") to inSimulateMode("some-test.json"), then it should be able to import the file corresponds to the test name.
This will be the issue for inCaptureOrSimulateMode as well.
I used a workaround for this one. The drawback of this approach is that it restarts the proxy between each tests (which is slow). I would definitively like a solution for this one.
public class LauncherPerTestHoverflyRule implements TestRule {
private final String destination;
public LauncherPerTestHoverflyRule(String destination) {
this.destination = destination;
}
@Override
public Statement apply(Statement base, Description description) {
final HoverflyRule hoverflyRule = createHoverflyProxy(getSimulationFileName(description), destination);
return hoverflyRule.apply(base, description);
}
private String getSimulationFileName(Description description) {
return description.getTestClass().getSimpleName().toLowerCase() + "/" + description.getMethodName().toLowerCase() + ".json";
}
}
@ia3andy, I think this is due to the limitation of JUnit 4. Ideally, we want to start and stop Hoverfly for the whole test suite once, and also be able to import/export different simulation per test. There are a few options I can think of:
- Call Hoverfly API manually
- Use JUnit 5 HoverflyExtension
- Use
@ClassRuleto manage Hoverfly lifecycle, and use a separate@Ruleto import/export simulation files for each test.
I manage to do it with your last proposition:
public class LauncherPerTestHoverflyRule implements TestRule {
private final HoverflyRule hoverflyRule;
public LauncherPerTestHoverflyRule(HoverflyRule hoverflyRule) {
this.hoverflyRule = hoverflyRule;
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (HoverflyMode.CAPTURE == hoverflyRule.getHoverflyMode()) {
hoverflyRule.capture("captured/" + getSimulationFileName(description));
} else {
hoverflyRule.simulate(defaultPath(getSimulationFileName(description)));
}
base.evaluate();
}
};
}
private String getSimulationFileName(Description description) {
return description.getTestClass().getSimpleName().toLowerCase() + "/" + description.getMethodName().toLowerCase() + ".json";
}
}
@ClassRule
public static final HoverflyRule HOVERFLY_RULE = LauncherHoverflyRuleConfigurer.createHoverflyProxy();
@Rule
public LauncherPerTestHoverflyRule hoverflyPerTestRule = new LauncherPerTestHoverflyRule(HOVERFLY_RULE);
The only drawback is that you need to have an empty recording for the HoverflyRule creation.
You can close
@ia3andy That's a very nice workaround! Would be good to add your solution to the project.
@tommysitu I will focus on the multiple requests issue, and then if I still got time propose a PR for this one too ;)