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

Question: Is there a way to use Category/MatchingRuleCategory with au.com.dius.pact.consumer 4.6.3? #1723

Open vasile21 opened 11 months ago

vasile21 commented 11 months ago

Hi Currently I am trying to migrate from the old pact-jvm-consumer-junit5 (version 4.0.10) to the new au.com.dius.pact.consumer (version 4.6.3)

I noticed that in the examples and documentation for the new library there is no example/usage of the approach to define matchingRules via Category or MatchingRuleCategory. eg MatchingRuleCategory categoryBody = new MatchingRuleCategory("body"); categoryBody.addRule("$.testField", new RegexMatcher(".+")); -> this does not generate matchingRules section in pact.

The examples with LambdaDsl. newJsonBody or PactDslJsonBody seems quite cumbersome to use on responses with a lot of different data types and various objects structure.
Could you point me towards an pact-consumer test / documentation that is still using the more friendly way (imo) of defining matchingRules for response body?

vasile21 commented 11 months ago

Update: PactDslRootValue receivedResponse= new PactDslRootValue(); receivedResponse.setValue(response); MatchingRuleCategory category = receivedResponse.getMatchers(); -> generates matchingRules section in the pact json file but I am waiting for your view on this approach

rholshausen commented 10 months ago

You need to set the values for the matching rules from the Pact object. This code

MatchingRuleCategory categoryBody = new MatchingRuleCategory("body");
categoryBody.addRule("$.testField", new RegexMatcher(".+")); 

will just create a new MatchingRuleCategory that is not associated with anything.

vasile21 commented 10 months ago

protected PactDslRootValue myMatcherRules(String response) { final PactDslRootValue expectedResponse = new PactDslRootValue(); expectedResponse.setValue(response); MatchingRuleCategory matchers = expectedResponse.getMatchers(); categoryBody.addRule("$.testField", new RegexMatcher(".+")); this seems to attach defined rules to body category