3

I tried with the following json but the wiremock doesn't recognize my change. I read the documentation of wiremock and I saw that they said: JSON equality matching is based on JsonUnit and therefore supports placeholders. I also tried with both JDK 8 and JDK 13 but both are not working

Below is the detail

    "method" : "POST",
    "bodyPatterns" : [{
      "equalToJson" : {
        "recipient": {
           "address": {
              "city": "Bellevue",
              "postalCode": "52031",
              "countryCode": "US"
           }
        },
        "sender": {
           "address": {
              "city": "",
              "postalCode": "",
              "countryCode": "HK"
           }
        },
        "shipDate": "${json-unit.any-string}",
        "accountNumber": {
           "key": ""
        }
      },

Result when running selenium test with mock (I executed mock via java -jar tmp/wiremock.jar --global-response-templating --root-dir ./mock --port 1337 ):

                                                           |
{                                                          | {                                                   <<<<< Body does not match
  "recipient" : {                                          |   "recipient" : {
    "address" : {                                          |     "address" : {
      "city" : "Bellevue",                                 |       "city" : "Bellevue",
      "postalCode" : "52031",                              |       "postalCode" : "52031",
      "countryCode" : "US"                                 |       "countryCode" : "US"
    }                                                      |     }
  },                                                       |   },
  "sender" : {                                             |   "sender" : {
    "address" : {                                          |     "address" : {
      "city" : "",                                         |       "city" : "",
      "postalCode" : "",                                   |       "postalCode" : "",
      "countryCode" : "HK"                                 |       "countryCode" : "HK"
    }                                                      |     }
  },                                                       |   },
  "shipDate" : "${json-unit.any-string}",                  |   "shipDate" : "May-26-2020",
  "accountNumber" : {                                      |   "accountNumber" : {
    "key" : ""                                             |     "key" : ""
  }                                                        |   }
}                                                          | }
                                                           |

Can anybody make some suggestions here. Thank you for reading my question

Sang Mai
  • 61
  • 2
  • 4

2 Answers2

1

you have to enable the placeholder as below and you should make sure you are using the jre-standalone jar. you seem to be using the normal standalone jar

"enablePlaceholders" : true

Mano
  • 11
  • 1
  • yes, I am. But I don't know how to do that :D it is good if you can guide us here – Sang Mai Dec 04 '20 at 16:27
  • I found https://stackoverflow.com/a/61352988/2621917 and used the suggestion: Update Wiremock. My version had placeholders documented, but did not work. – Michael Piefel Jan 14 '21 at 10:43
1

The usage of "${json-unit.any-string}" is right. But placeholder works when the right dependency is used.

Using dependency com.github.tomakehurst:wiremock-jre8 worked for me.

Refer https://wiremock.org/docs/request-matching/ for more info. This would mention the following note

Placeholders are only available in the jre8 WireMock JARs, as the JsonUnit library requires at least Java 8.