hoverfly-java icon indicating copy to clipboard operation
hoverfly-java copied to clipboard

HoverflyRule doesn't export simulations for multiple tests

Open tommysitu opened this issue 8 years ago • 7 comments

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.json
  • booking_service_test_shouldDeleteBooking.json

tommysitu avatar Jun 21 '17 10:06 tommysitu

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.

tommysitu avatar Jun 21 '17 13:06 tommysitu

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 avatar Mar 12 '18 10:03 ia3andy

@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 @ClassRule to manage Hoverfly lifecycle, and use a separate @Rule to import/export simulation files for each test.

tommysitu avatar Mar 12 '18 11:03 tommysitu

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.

ia3andy avatar Mar 23 '18 16:03 ia3andy

You can close

ia3andy avatar Mar 23 '18 16:03 ia3andy

@ia3andy That's a very nice workaround! Would be good to add your solution to the project.

tommysitu avatar Mar 23 '18 17:03 tommysitu

@tommysitu I will focus on the multiple requests issue, and then if I still got time propose a PR for this one too ;)

ia3andy avatar Mar 23 '18 17:03 ia3andy