hoverfly-java: [0.14.0] Working directory doesn't seem to be relative to workspace directory

Description of the bug

The working directory of the hoverfly instance seems to be located in the temproary folder instead of the workspace/project directory. And since hoverfly requires relative paths this makes it almost impossible to reliably specify your response files let alone specify them in a way that is not machine dependent.

Instead the working directory should in the default path (src/test/resources/hoverfly) or where the simulation file is so relative paths can be used easily

Steps to reproduce the issue

Start a simulation from a simulation file. Specify a path relative to the default path or the simulation JSON.

Example gist: https://gist.github.com/BrainStone/14b5facb30a17e3d0c658d12ba49796d

Observed result

Hoverfly errors out:

[02:05:12.589][Test worker][WARN ][i.s.h.junit.api.HoverflyClient]: Failed to set simulation: Unexpected response (code=500, message={"error":"An error occurred: open test.json: The system cannot find the path specified."}{"data":{"pairs":[],"globalActions":{"delays":[],"delaysLogNormal":[]}},"meta":{"schemaVersion":"v5.1","hoverflyVersion":"v1.3.1","timeExported":"2020-11-29T02:05:12+01:00"}}
)

Expected result

The simulation just working fine and being able to find the files.

Additional relevant information

  • Hoverfly Java version: 0.13.2-SNAPSHOT - 0.14.0
  • OS: Windows 10

Only way I was able to get it working was using a pseudo absolut path. So just a bunch of ../../../.. until you eventually reach the root dir, then specify the path like you would with an absolute path, excluding the drive specification on Windows (needs to be on the same drive)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

bodyFile is not properly supported in hoverfly-java yet, I can take a look

That worked great!

Looking forward to having that being done automatically.

For people that might stumble across this in the future, here is my code:

public class TestApi {
  private static final String simulationFile = "simulation.json";

  @ClassRule
  public static final HoverflyRule hoverflyRule =
      HoverflyRule.inSimulationMode(
          SimulationSource.defaultPath(simulationFile),
          HoverflyConfig.localConfigs()
              .addCommands(
                  "-response-body-files-path",
                  getPathOfResource(simulationFile).getParent().toAbsolutePath().toString()));

  private static URL findResourceOnClasspath(String resourceName) {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    return Optional.ofNullable(classLoader.getResource(resourceName))
        .orElseThrow(
            () -> new IllegalArgumentException("Resource not found with name: " + resourceName));
  }

  @SneakyThrows(URISyntaxException.class)
  private static Path getPathOfResource(String resource) {
    return Paths.get(
        findResourceOnClasspath(HoverflyConstants.DEFAULT_HOVERFLY_RESOURCE_DIR + '/' + resource)
            .toURI());
  }
}

(@SneakyThrows is from lombok. If you don’t have that, just catch the exception and rethrow it wrapped in a RuntimeException)

Ok. Will give that a shot. Thanks very much!