Search code examples
java-8wiremockwiremock-standalone

How to use mappingsloader in wiremock to load mappings file(s)?


I have created my mappings by recording the server's transactions. Now I want to use my json mapping file(s) to load the mappings. For that I have the code below:

import com.github.tomakehurst.wiremock.WireMockServer;

public class mockedTests{
    public static WireMockServer mockingServer;
    
    public void loadMaps(){
        mockingServer.loadMappingsUsing(****);
    }
}

I checked the wiremock code and the loadMappingsUsing method needs a MappingsLoader as its parameter. However, I can't find a way to create a MappingsLoader properly. I couldn't find anything in the documentation and I can't find any examples nor tutorials online. Furthermore, I am stuck with using a WireMockServer object rather than a rule or a WireMock object. Does anyone know how I can create a MappingsLoader to load my mappings json file(s)? I am using the standalone version of wiremock. Thanks.


Solution

  • By default, WireMock will look for files under /src/test/resources. If you are creating your WireMock instance in Java and your files in a custom location, you'll need to pass in an option() object, with the .usingFilesUnderDirectory("/path/to/files-and-mappings-root") attached

    WireMockServer wm = new WireMockServer(options().usingFilesUnderDirectory("/path/to/files-and-mappings-root"))
    

    If you need to do this while starting WireMock from the command line, it would be something like:

    $ java -jar wiremock-standalone-2.27.0.jar --root-dir "/path/to/files-and-mappings-root"
    

    I think your original issue may be coming from not starting your WireMock server or rule with an options() object.