concept predicategenerator in category microservice

appears as: predicateGenerators, predicateGenerators, The predicateGenerators
Testing Microservices with Mountebank

This is an excerpt from Manning's book Testing Microservices with Mountebank.

If you needed a predicate that matched the career field of the last element of the identities array, then you could use the same JSONPath selector you saw in the previous chapter. Because you now want mountebank to generate the predicate, you specify the selector in the predicateGenerators object and rely on the proxy to fill in the value, as in the following listing.

Listing 5.6. Specifying a jsonpath predicateGenerators
{
  "proxy": {
    "to": "http://localhost:8080",
    "predicateGenerators": [{
      "jsonpath": {                                       #1
        "selector": "$.identities[(@.length-1)].career"   #1
      },                                                  #1
      "matches": { "body": true }                         #2
    }]
  }
}

Remember, predicateGenerators work on the incoming request, so the JSONPath selector saves off the value in the request body. If you sent the Abagnale JSON in listing 5.6 to your proxy, the generated stub would look something like this:

5.2.1. Creating predicates with predicateGenerators

Your inventory service includes the product id on the path, so sending a GET to /inventory/2599b7f4 returns the inventory for product 2599b7f4, and a GET to /inventory/e1977c9e returns inventory for product e1977c9e. Let’s augment the proxy definition you set up in listing 5.1 to save the response for each product separately. Because it’s the path that varies between those requests, you need to tell mountebank to create a new is response with a path predicate. You do so using a proxy parameter called predicateGenerators. As the name indicates, the predicateGenerators are responsible for creating the predicates on the saved responses. You add an object for each predicate you want to generate underneath a matches key, as in the following listing.