Skip to content

Instantly share code, notes, and snippets.

@isalevine
Last active February 19, 2021 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isalevine/87ffb2e331ce48f7cc7a232c2c457de1 to your computer and use it in GitHub Desktop.
Save isalevine/87ffb2e331ce48f7cc7a232c2c457de1 to your computer and use it in GitHub Desktop.
Pact JVM -- sample code of two interactions under the same state
// interaction setup code
RequestResponsePact pact =
builder
.given("default state")
.uponReceiving("id less than or equal to 9000")
.path("/api/multipleInteractions")
.method("GET")
.matchQuery("id", "\\d", "1")
.willRespondWith()
.status(200)
.body(newJsonBody(
(o) -> {
o.object(
"payload",
(p) -> p.stringValue("description", "id is 1"));
})
.build()
)
.given("default state")
.uponReceiving("id greater than 9000")
.path("/api/multipleInteractions")
.method("GET")
.matchQuery("id", "\\d+", "9001")
.willRespondWith()
.status(200)
.body(newJsonBody(
(o) -> {
o.object(
"payload",
(p) -> p.stringValue("description", "id is OVER 9000!!!!"));
})
.build()
)
.toPact();
// test code executing requests (both used in same test method)
String url = multipleInteractionsProvider.getUrl() + "/api/multipleInteractions?id=1";
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(request);
...
String url2 = multipleInteractionsProvider.getUrl() + "/api/multipleInteractions?id=9001";
HttpGet request2 = new HttpGet(url2);
CloseableHttpResponse response2 = httpClient2.execute(request2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment