I deploy WireMock as a single WAR to my server. I followed the provided sample-war project to make my WAR works. That was very useful, so thanks for this example.
The only one problem with my app is that WireMock reads mapping and configuration files from inside the WAR.
The path of these files is configurable via the WireMockFileSourceRoot context-param in the web.xml file but it only works if the path points to a directory which locates in the WAR.
I would like to keep mapping and response files in a separate folder on the disk.
I have checked the source code and as I can see WireMock use ServletContextFileSource class to read mapping files from the servlet context. This class extends AbstractFileSource.
I have found a good candidate SingleRootFileSource which also extend the same abstract class and it seems for me that this class can read mapping files from any directory from the disk BUT I have no idea how to set up WireMock to use this class instead of the default.
Could you please put me to the right direction?
First, you need to convert files read by SingleRootFileSource to StubMappings. Eg:
SingleRootFileSource stubsFolder = new SingleRootFileSource("my/path/to/jsons/with/recorded/stubs")
stubsFolder.listFilesRecursively().each { TextFile stubFile ->
StubMapping mapping = StubMapping.buildFrom(stubFile.readContentsAsString());
...
Then you need to register them in Wiremock server. There is couple of ways how it can be done. Both wiremock server and client can register stubs. Dunno how you run tests, but if you need different stubs per test case, you possibly would have to register stubs use client (using com.github.tomakehurst.wiremock.client.WireMock.stubFor). In case you decide to use stubFor you have to introduce simple adapter that converts StubMapping to MappingBuilder, as stubFor is taking only MappingBuilder as parameter. Sample adapter:
public class MappingBuilderAdapter implements MappingBuilder {
StubMapping stubMapping;
@Override
public StubMapping build() {
return this.stubMapping;
}
...
Client has to be connected to server before invoking stubFor. Checkout configureFor method to connect from wiremock client, described in [Configuring WireMock using the Java client]: http://wiremock.org/docs/running-standalone/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With