2

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.

Mansour.M
  • 500
  • 4
  • 18
  • 1
    Are you sure that you are running the standalone version of WireMock? It looks like you're using the Java version. How exactly are you starting WireMock? – agoff Jul 22 '20 at 13:13
  • 1
    I am using the standalone Java version. I start the server by running `mockingServer.start()` in my junit `@BeforeClass` method. This is the version I am using: https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock-standalone/2.27.1 – Mansour.M Jul 22 '20 at 14:52

1 Answers1

3

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.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • 1
    When I create a `WireMockServer` it seems to ignore the default location and there is no method to configure the new location with. Or I have not found one yet. I have to use `WireMockServer` because the other methods are conflicting with spring boot environment and I cannot change that. – Mansour.M Jul 22 '20 at 14:55
  • What do you mean by "ignore the default location"? When you start the server, if you ping it to get the current mappings, I'm guessing it would be empty? You might have placed your mappings in an unrecognized place. Did you try changing the root directory? I think you'd use the `.usingFilesUnderDirectory()` method? – agoff Jul 22 '20 at 15:46
  • 1
    Strangely enough, it does not read from the default directory. It doesn't read from anywhere. It just starts a server without any maps being loaded. Furthermore, to my dismay a `WireMockServer` object does not have a `.usingFilesUnderDirectory()` method. Only a `WireMockRule` has it. It was pretty unexpected to me too. – Mansour.M Jul 22 '20 at 17:18
  • 1
    Do you have a `/src/test/resources/` folder with your mappings and __files directories, relative to either where the `WireMockServer` code lives OR where the code is being called? And you're trying to create the `WireMockServer` with the `.usingFilesUnderDirectory()` like this: `WireMockServer wm = new WireMockServer(options().usingFilesUnderDirectory("/path/to/directory"));` ? – agoff Jul 23 '20 at 13:22
  • 2
    First one yes... second one no! I have the directory, I was not aware of `options().usingFilesUnderDirectory("/path/to/directory")`! I have read many tutorials and sample codes but its the first time I'm seeing `options()`! Let me try it and be back! – Mansour.M Jul 23 '20 at 13:54
  • 1
    Ok, silly question... but where do I import 'options()' from? I found a static one at `com.github.tomakehurst.wiremock.client.WireMock.options` but it does not have a `.usingFilesUnderDirectory()`. – Mansour.M Jul 23 '20 at 14:16
  • 2
    Found it! It is `import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;` – Mansour.M Jul 23 '20 at 14:18
  • 1
    Yeah, it is at the top of the configuration documentation: http://wiremock.org/docs/configuration/ . Did switching to using `options().usingFilesUnderDirectory()` solve your issues? – agoff Jul 23 '20 at 16:35
  • 1
    I don't know how and why but I had totally missed the configuration doc page! Yep, that fixed it. Thank you very much for your help. You are the best! If you post it as a response I'll select it as the correct response. Thanks again! – Mansour.M Jul 23 '20 at 16:39
  • 1
    Awesome, glad to hear. I'll just update this answer to include the information we discussed if that is fine. – agoff Jul 24 '20 at 13:25