Ruud Welling
03/19/2025, 4:36 PM@Test
@PactTestFor(pactMethod = "mySyncMessagePact")
void mySyncMessagePact(V4Interaction.SynchronousMessages interaction) {
String request = "{}"; // 1: should call a method that produces the request
// 2: Validate that the request is okay according to the pact
// 3: Validate that the response message can be handled
}
How do I validate that the request message is compliant with the pact that I specified?
After looking at the library code I could not find a standard utility that allows me to easily validate thisrholshausen
03/19/2025, 10:15 PMRuud Welling
03/20/2025, 12:42 PMprivate void handleInteractionWithoutResponse(V4Interaction.SynchronousMessages interaction, Object requestBody) {
var requestBodyAsString = JsonSupport.toJson(requestBody);
var request = interaction.getRequest();
var matcher = MatchingConfig.lookupContentMatcher(request.getContentType().getBaseType());
var actualRequest = OptionalBody.body(requestBodyAsString);
var matchResult = matcher.matchBody(request.getContents(), actualRequest,
new MatchingContext(request.getMatchingRules().rulesForCategory("body"), false));
if (!matchResult.matchedOk()) {
String errorMessage = matchResult.getMismatches().stream().map(m -> m.description()).collect(Collectors.joining("\n"));
throw new AssertionError("Request did not match: " + errorMessage);
}
assertThat(interaction.getResponse())
.hasSize(0);
}
But I was actually hoping that something like this was already part of the pact SDKrholshausen
03/20/2025, 10:04 PMRuud Welling
03/21/2025, 7:22 AM