Search code examples
gradlepact

Provider side of PACT using gradle


I am new to PACT. My application is a spring boot application. Consumer has pact created in PACT broker. Now I am trying to run tests from providers end and I am using gradle plugin to run tests. I have couple of questions:

  1. Pact published by consumer do not have any states specified. So this means I don't have to use any stateChangeUrl at provider side ?

  2. Pact published has get method. However when I run pactVerify server starts and my pact tests are failing because I don't have any existing data. How can I inject data after.

What I am trying to achieve is: When I say pactVerify

  • Server starts up, and my spring boot app should be up and running in localhost (This is good)
  • Before pacts are verified I need to inject some data like "POST\add User. So that pact test passes when it tries to do GET\User. (How to do this ?)

Below is my gradle file

pact {
    serviceProviders {
        'user-api' {
            protocol = 'http'
            host = 'localhost'
            port = 8080

            startProviderTask = startService
            terminateProviderTask = stopService

            if ('pactVerify' in gradle.startParameter.taskNames) {
                hasPactsFromPactBroker('http://pactbroker.com') {
                < How to write some code here to say do POST before running PACT >

                }

            }

        }
    }

Solution

    1. Pact published by consumer do not have any states specified. So this means I don't have to use any stateChangeUrl at provider side ?

    It means you have chosen not to use this mechanism to change states, and therefore yes, you don't need to use that URL. But you may wish to change your mind on that.

    1. Pact published has get method. However when I run pactVerify server starts and my pact tests are failing because I don't have any existing data. How can I inject data after.

    Why not inject the data before you start all of the tests? If you don't have states to worry about, then you may as well just start the provider with the data pre-loaded into the database.

    As noted, all of the configuration info can be found at https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-gradle.