Search code examples
javawiremock

Cannot deserialize instance of `java.lang.String` out of START_ARRAY token exception received in Wiremock Json file configuration


I am trying to mock few API's using WireMock standalone jar with JSON file configuration.

Below is the JSON file containing multiple stub mappings which I'm using-

{
  "mappings": [{
      "request": {
        "method": "GET",
        "url": "/users"
      },
      "response": {
        "status": 200,
        "body": [{
            "id": 1,
            "name": "Leanne Graham",
            "username": "Bret",
            "email": "[email protected]",
            "address": {
              "street": "Kulas Light",
              "suite": "Apt. 556",
              "city": "Gwenborough",
              "zipcode": "92998-3874",
              "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
              }
            },
            "phone": "1-770-736-8031 x56442",
            "website": "hildegard.org",
            "company": {
              "name": "Romaguera-Crona",
              "catchPhrase": "Multi-layered client-server neural-net",
              "bs": "harness real-time e-markets"
            }
          },
          {
            "id": 2,
            "name": "Ervin Howell",
            "username": "Antonette",
            "email": "[email protected]",
            "address": {
              "street": "Victor Plains",
              "suite": "Suite 879",
              "city": "Wisokyburgh",
              "zipcode": "90566-7771",
              "geo": {
                "lat": "-43.9509",
                "lng": "-34.4618"
              }
            },
            "phone": "010-692-6593 x09125",
            "website": "anastasia.net",
            "company": {
              "name": "Deckow-Crist",
              "catchPhrase": "Proactive didactic contingency",
              "bs": "synergize scalable supply-chains"
            }
          }
        ],

        "headers": {
          "Content-Type": "application/json"
        }
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/posts?userId=1"
      },
      "response": {
        "status": 200,
        "body": "[]"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/comments?postId=1"
      },
      "response": {
        "status": 200,
        "body": "[]"
      }
    }
  ]
}

When I am trying to start Wiremock server using standalone jar (wiremock-standalone-2.24.1.jar) I am getting below exception, I verified the Json schema and it looks ok.

Wiremock msrivast$ java -jar wiremock-standalone-2.24.1.jar Exception in thread "main" com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /Users/msrivast/Downloads/Wiremock/./mappings/results.json: Cannot deserialize instance of java.lang.String out of START_ARRAY token at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:121) at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:199) at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:195) at com.github.tomakehurst.wiremock.core.WireMockApp.(WireMockApp.java:103) at com.github.tomakehurst.wiremock.WireMockServer.(WireMockServer.java:72) at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:65) at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:125)


Solution

  • body parameter always expects a String in WireMock. It worked once I passed my response in body as a String.