Hello. I am trying to create a synchronous message...
# pact-jvm
r
Hello. I am trying to create a synchronous message pact consumer test. I have defined the pact context and am working on the corresponding consumer test:
Copy code
@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 this
r
@rholshausen Thanks for sharing this. I had already have a look at this class before. But what I see is that the "mockProvider" in this code does not actually validate the request pact. So it is not like http based pacts where the mock-server verifies that the actual request matches the request in the pact. I currently wrote some manual code as a workaround:
Copy code
private 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 SDK
r
Pact will create the request message, so why would it need to verify it?
r
To create an http consumer pact, you first define the pact with both request and response, and then in the corresponding consumer test you call the mock sever (created by the SDK) to prove that you can indeed produce a request as specified and handle the response. When the http request is not as you specified it the sdk will fail the test. I am trying to reproduce the same behaviour for sync message pacts, where the consumer pact test would need to produce the message, and only after validating that message would the mock provider produce the reponse. Since this is a consumer test, I believe we should validate the request message, like we do in http based pacts. If we don't validate the actual request that the consumer application sends, then you can define anything in the consumer request pact, and have the consumer tests succeed.