Search code examples
pact

How to use Pact Matcher for values other than 'Strings'


In pact-jvm (groovy on consumer side and gradle on the provider side), I'm trying to use Pact matchers like below:

name regexp(~/\w+/,'sony')

Will Pact matcher regex work only for Strings ? For values other than strings, do I need to use Pact term ?

If the answer is 'yes' for the above two questions. Please explain me how to use Pact term in groovy style.

I have tried using Pact term like below:

date Pact.Term(generate :"02/11/2013", matcher:/\d{2}\/\d{2}\/\d{4}/)

But getting groovy - MethodMissingException. My complete response body for reference:

withBody {
                id regexp('[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}','e8cda07e-849f-49c2-94d6-aaa5c4ab7fcd')
                name regexp(~/\w+/,'sony')
                date Pact.Term(generate :"02/11/2013", matcher:/\d{2}\/\d{2}\/\d{4}/)
        }

Solution

  • Regex only applies to strings.

    If you want to match a date, I'd suggest using the date matcher, e.g.

    withBody {
                    id regexp('[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}','e8cda07e-849f-49c2-94d6-aaa5c4ab7fcd')
                    name regexp(~/\w+/,'sony')
                    date date("dd/MM/yyyy", "02/11/2013")
             }
    

    See the available DSL methods https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-groovy#dsl-methods-1