0

I am trying to post mappings to a remote server from a spring application. What I found while debugging is that my JSON gets converted to "StubMapping" and this is the place where the code is failing with the following error.

Error creating bean with name 'wiremockConfig' defined in file [C:\Users\Addy\school-impl-api\target\classes\com\test\school\project\wiremock\WiremockConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.test.order.implementation.product.wiremock.WiremockConfig$$EnhancerBySpringCGLIB$$b100848d]: Constructor threw exception; nested exception is com.github.tomakehurst.wiremock.common.JsonException: {
  "errors" : [ {
    "code" : 10,
    "source" : {
      "pointer" : "/mappings"
    },
    "title" : "Error parsing JSON",
    "detail" : "Unrecognized field \"mappings\" (class com.github.tomakehurst.wiremock.stubbing.StubMapping), not marked as ignorable"
  } ]
}

I got details for posting to a remote standalone server from the following issue (last comment).

https://github.com/tomakehurst/wiremock/issues/1138

My code for posting to the remote server is like this:

WireMock wm = new WireMock("https", "wiremock-poc.apps.pcf.sample.int", 443);
wm.loadMappingsFrom("src/main/resources"); // Root dir contains mappings and __files

This gets loaded when I run the profile local.

Please provide your guidance on how to solve this and move further.

Regards

Update: Sample mapping file.

        {
  "mappings": [
    {
      "request": {
        "method": "GET",
        "urlPathPattern": "/school/admin/rest/users/([0-9]*)?([a-zA-Z0-9_\\-\\=\\?\\.]*)"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "bodyFileName": "./mockResponses/School-getUser.json"
      }
    }
  ]
}
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • Could you please add example how does your `json` file with wiremock mapping looks like. It looks not complaint with docs: http://wiremock.org/docs/stubbing/ – Mikhail Kopylov Apr 28 '20 at 09:32
  • @MikhailKopylov just updated the mapping file example. – Bilbo Baggins Apr 28 '20 at 09:35
  • Seems right: there should not be `mappings` key, just put `request` and `response` in root – Mikhail Kopylov Apr 28 '20 at 09:38
  • http://wiremock.org/docs/stubbing/ on the same page an example with Mappings exists. I am not sure, why it is not able to parse it into list of StubMapping. – Bilbo Baggins Apr 28 '20 at 09:40
  • That example is for bulk importing, while in your example it seems every file in `resources` is expected to be a separate mapping Just try and see if it works :) – Mikhail Kopylov Apr 28 '20 at 09:41
  • okay, @MikhailKopylov I have created multiple files just for the management of JSON file becomes easy. but for one API I would like to have multiple mappings available as well, and for that reason I have used Mappings in JSON, is it not possible to provide multiple mappings for a single API and expect different result for different mapping? – Bilbo Baggins Apr 28 '20 at 09:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212691/discussion-between-mikhail-kopylov-and-bilbo-baggins). – Mikhail Kopylov Apr 28 '20 at 11:30

1 Answers1

0

After a discussion in chat found out that it's supported to keep each mapping in a separate file. Here's the source code that is responsible for that: RemoteMappingsLoader#load

Mikhail Kopylov
  • 2,008
  • 5
  • 27
  • 58