pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 479 forks source link

Pending interactions in pact-jvm #1707

Closed holomekc closed 1 year ago

holomekc commented 1 year ago

Hi, I found this issue pact-foundation/pact-specification/issues/73 and I was wondering how I can set this in pact-jvm. I could find that the model contains the value: https://github.com/pact-foundation/pact-jvm/blob/bb4010b01146eb05f3efc0602fdf0f99719b5c80/core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt#L139 But I am not sure how I can set this in my consumer test. I searched in the @Pact annotation and also via the PactDslWithProvider, which we are using to define our expectations. But I could not find a way to do so.

What is the appropriate way to set this pending state?

Br, Chris

PS: Is there a documentation regarding this value? I can only find the pending pacts docs, but nothing regarding pending interactions set by the consumer.

rholshausen commented 1 year ago

If you use the PactBuilder class, and one of the expectsToReceive methods (i.e. expectsToReceiveHttpInteraction), the child builders have a method to set the pending flag.

For example:

.expectsToReceiveHttpInteraction("V4 PactProviderTest test interaction", httpBuilder -> {
        return httpBuilder
          .withRequest(requestBuilder -> requestBuilder
            .path("/")
            .method("GET"))
          .willRespondWith(responseBuilder -> responseBuilder
            .status(200)
            .body("{\"responsetest\": true, \"version\": \"v3\"}")
            .header("test", notEmpty("Example"))
          )
          .pending(true); // <---
      })
holomekc commented 1 year ago

Thx for the incredible fast feedback. This works. Thank you so much.

holomekc commented 1 year ago

Hi Sorry to reopen this again. The PactBuilder indicates that PactDslWithProvider and MessagePactBuilder are legacy, but the SynchronousMessagePactBuilder is not? https://github.com/pact-foundation/pact-jvm/blob/a91598b74742771b3cff4f7f3b4a6b5dbed97cc9/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactBuilder.kt#L82-L101

But then how to achieve the same with Async? I can use the PactBuilder and set the pending flag, but then I am facing issues with creating the content of my expectations.

I could find: /**

The other option is: https://github.com/pact-foundation/pact-jvm/blob/a91598b74742771b3cff4f7f3b4a6b5dbed97cc9/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactBuilder.kt#L246-L255

but then I would need to guess message.contents and write alle matching rules as text. https://github.com/pact-foundation/pact-jvm/blob/a91598b74742771b3cff4f7f3b4a6b5dbed97cc9/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactBuilder.kt#L268-L282

Is there a way to use a builder here and not just a map?

rholshausen commented 1 year ago

I've added the missing methods to SynchronousMessagePactBuilder.

You can also use PactBuilder.expectsToReceiveSynchronousMessageInteraction which works in the same way as the other interaction builders.