What would be really nice is to be able to package mock files in a jar and share them. This doesn't seem possible at the moment. Is this planned?
It's not documented at the moment, but there is ClasspathFileSource which I think might do what you're looking for.
I'm also missing this feature. I've tried the usingFilesUnderClasspath configuration option, but it doesn't seem to work when I run my app as a jar.
You're going to have to give me a bit more to go on than that. What error are you seeing? Code? Sure the files are in the right place in the jar?
Sorry for the lack of details.
This is my little project. A spring Boot application (on port 9999) which starts a WireMock server (port 8080). Configuration: WireMockService
When I run it from IDE (IntelliJ) everything works fine. But when I try to run it as a jar, I get an exception:
http://pastebin.com/Sf6ZChLV
For example http://localhost:8080/hello produces this error.
Thanks for all the detail. Investigating.
Your build puts wiremock/__files under BOOT-INF/classes in the JAR file. I'm afraid I don't know much about how Spring Boot manages the classpath, but if you can persuade it to place those dirs under the root (as with an ordinary JAR build) that might fix it.
Hi All,
I want to reopen this issue.
I'm building a simple fat jar (jar with dependencies) which contains the __files and mappings in the classpath (under a folder called stubs). I can see that wiremock is not responding with any of these files but I could see that it has picked the right resource - jar.
This is my configuration
ClasspathFileSource fileSource = new ClasspathFileSource("stubs/");
mockServer = new WireMockServer(port, fileSource, false);
When i peek into the classpathfileresource, I can see it pick the right jar
System.out.println(fileSource.getUri());
Output:
jar:file:/path/to/fat_jar.jar!/stubs/
Can someone tell me the right way to go about this?
Hi,
I got this working.
The thing is for a FileSystemFileResource, redundant "/"(slashes) still resolve to the file
Eg. stubs//mappings
But for a classpathfileresource, using jars, redundant slashes lead to issues.
I would still raise this as a bug.
@tomakehurst ,
What do you think?
I guess we could strip redundant slashes in WireMock. I doubt I'll manage to work on this any time soon, but I'm happy to receive a PR.
This issue is still consist?
Yeah, nothing has happened since the last comment. My offer to merge a PR still stands.
Hi,
I use the following simple workaround.
WireMockServer wireMockServer = new WireMockServer(options().fileSource(new ClasspathFileSourceWithoutLeadingSlash()));
wireMockServer.start();
/*
* Without this class Wiremock tries to find the mappings directory under /mappings and the classloader will not find this
* directory because of the leading slash. This class removes the leading slash and as a consequence the classloader
* will find the mappings directory.
*/
class ClasspathFileSourceWithoutLeadingSlash extends ClasspathFileSource {
ClasspathFileSourceWithoutLeadingSlash() {
super("");
}
@Override
public FileSource child(String subDirectoryName) {
return new ClasspathFileSource(subDirectoryName);
}
}
Thanks @BoukeNijhuis
THANK YOU @BoukeNijhuis !
I have been losing my mind trying to understand why Wiremock was able to load mappings just fine locally but was completely unable to after deployment to PCF. Even when I was POSITIVE I had the right path. Anyway, this resolved my issues. Thank you so much.
PR attempt: https://github.com/tomakehurst/wiremock/pull/944
There are two different issues discussed in this thread. Regarding the original issue, i.e. the Spring Boot problem, you can solve it by adding this to your pom. In my case, the final jar is called "app.jar", you might need to configure that. And I have the files under src/main/resources/wiremock.
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<!-- This is needed because Wiremock requires these files to be at the root of the JAR,
not under BOOT_INF. See https://github.com/tomakehurst/wiremock/issues/504 -->
<execution>
<id>addWiremockFilesOnRootLevel</id>
<phase>package</phase>
<configuration>
<target>
<zip destfile="${project.build.directory}/app.jar" update="yes" compress="false">
<zipfileset dir="${project.build.directory}/classes/wiremock" prefix="wiremock"/>
</zip>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
For the initial issue with Spring Boot, I was able to get usingFilesUnderClasspath to work by prefixing the path with BOOT-INF/classes. My mapping files are under src/main/resources/wiremock so instead of using wiremock as the path, I modified it to BOOT-INF/classes/wiremock.
@vivin thanks for sharing this. Good to know there's a workaround.
Most helpful comment
Hi,
I use the following simple workaround.