Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiremock Placeholder isn't recognized

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

like image 451
Sang Mai Avatar asked Nov 03 '25 12:11

Sang Mai


2 Answers

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

like image 58
Mano Avatar answered Nov 06 '25 02:11

Mano


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.

like image 39
Bhuvana Sai B Avatar answered Nov 06 '25 03:11

Bhuvana Sai B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!