Pact-jvm: @PactFolder doesn't work correctly with absolute path

Created on 2 Apr 2016  路  6Comments  路  Source: pact-foundation/pact-jvm

Simple switching from @PactFolder to @PactUrl makes it work

  • Working: @PactUrl(urls = {"file:///Users/HL/tmp/pacts/hello_consumer-hello_provider.json"})
  • Not working: @PactFolder("/Users/HL/tmp/pacts/hello_consumer-hello_provider.json")
    I noticed that when debugging, the "path" in the later case is read as empty value. I have discussed with @uglyog and here's his suggestion:

@luvLinux I have found the issue. @PactFolder loads the pacts from a resource folder via class.getClassLoader().getResource(). If you put your directory in a project resource folder (for example src/test/resources/pacts) and then use @PactFolder("pacts") it will work.

For reference, working source code is here, but I had to use @PackUrl
Test file in Java: https://gist.github.com/luvLinux/188394b94e98ad3716f9864d3a882f06
Pact file in JSON: https://gist.github.com/luvLinux/fc38200a2b08f767017b55eae2579a90

bug

Most helpful comment

@PactFolder loader will load pacts from a directory now.

All 6 comments

@PactFolder loader will load pacts from a directory now.

This has been released with versions 3.2.7 and 2.4.9

The fix does not work on Windows (tested with au.com.dius:pact-jvm-provider-spring_2.11:3.5.1).

When defining e.g. @PactFolder("src/test/resources/pact_contracts") then no pact files are found (java.lang.Exception: Did not find any pact files for provider Xyz)

The problem is in method au.com.dius.pact.provider.junit.loader.PactFolderLoader.resolvePath() on line 74:
    URL resourcePath = PactFolderLoader.class.getClassLoader().getResource(path.getPath());

path.getPath() returns "src\test\resources\pact_contracts" which results in resourcePath being set to "file:/C:/path/to/project/src%5ctest%5cresources%5cpact_contracts" which does not correspond with the folder defined in the annotation.

To fix this, the backslashes '\' in the value returned by path.getPath() have to be converted to forward slashes '/', e.g. path.getPath().replace('\\', '/')

Re-opening this to resolve issues with Windows.

I can not reproduce this issue (with the latest version). Have a look at AmqpTest, it works on Windows.

Unfortunately the issue explaned by @tspoeri still exists on Windows. I updated vom 2.4.19 to 3.5.15 and got "Did not find any pact files for provider". A quick debug confirms that /s result in %5cs in the resourcePath. My workaround was to move all pact file to a path without slashes...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jejuni picture Jejuni  路  4Comments

BramVanImpe picture BramVanImpe  路  6Comments

ajhcpt picture ajhcpt  路  9Comments

uglyog picture uglyog  路  4Comments

SchulteMarkus picture SchulteMarkus  路  5Comments