3

I am using wiremock-jre8-standalone-2.27.0 jar to mock an API. My mapping json looks like:

 {
  "request": {
    "url": "/sampleUrl",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath" : {
          "expression": "$[0].fruit",
          "contains": "apple"
        },
        "matchesJsonPath" : {
          "expression": "$[0].quantity",
          "contains": "1221"
        },
        "matchesJsonPath" : {
          "expression": "$[1].fruit",
          "contains": "banana"
        },
        "matchesJsonPath" : {
          "expression": "$[2].quantity",
          "contains": "2784"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "bodyFileName": "prices.json",
    "delayDistribution": {
      "type": "uniform",
      "lower": 200000,
      "upper": 500000
    }

As it could be seen there are 4 matchesJsonPath inside bodyPatterns but only the last matchesJsonPath ($[2].quantity == 2784) is compared every time. Is I change the remaining things in the request body such as it fails the first three matchesJsonPath and send the request through Postman I still get the response. Is there a way to make Wiremock check all the conditions?

  • 1
    Not entirely sure if this will solve your issue, but each comparison (matchesJsonPath) should be its own JSON object. Instead you have all comparisons in one JSON object. I'd try splitting them out and seeing if that works. Formatting will be weird, but something like `[ { "matchesJsonPath": { "expression": "test", "contains": "test" } }, { "matchesJsonPath": { "expression": "test2", "contains": "test2" } } ]` – agoff Sep 04 '20 at 13:31
  • 1
    Thanks @agoff. It is what worked for me. – Raghvendra Rao Sep 07 '20 at 08:22
  • Awesome. I'm going to add that as an answer. I would appreciate it if you selected that as the correct answer once I do. Thanks! – agoff Sep 08 '20 at 12:50

1 Answers1

2

The issue is with your bodyPatterns array. Each match needs to be its own JSON object within the array. You currently have the matchers all within one object.

"bodyPatterns": [
    {
        "matchesJsonPath" : {
          "expression": "$[0].fruit",
          "contains": "apple"
        }
    },
    {
        "matchesJsonPath" : {
          "expression": "$[0].quantity",
          "contains": "1221"
        }    
    },
    {
        "matchesJsonPath" : {
          "expression": "$[1].fruit",
          "contains": "banana"
        }    
    },
    {
        "matchesJsonPath" : {
          "expression": "$[2].quantity",
          "contains": "2784"
        }    
    }
]
agoff
  • 5,818
  • 1
  • 7
  • 20