0

When I execute my provider test from IDE @State method is executed, but mvn pact:verify command does not execute the @State of the provider

Provider test class:

@RunWith(PactRunner.class)
@Provider("wProvider")
@PactFolder("/home/username/workspace/automation/pactProject/pacts/")
public class WpproviderTest {

     @State("wProvider will create new stuff")
    public void providerState(){
         System.out.println("execute the the provider state");

         //some code...

        System.out.println("your new stuff response is: " + jsonResponse)   
    } 

     @TestTarget
    public final Target target = new HttpTarget(url, true);

    @TargetRequestFilter
    public void updateAuthHeaders(HttpRequest request) {
        request.addHeader("some auth headers")
    }
}

my pom.xml file:

<plugin>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-provider-maven_2.12</artifactId>
    <version>3.6.7</version>
    <configuration>
        <serviceProviders>
            <serviceProvider>
                <name>wProvider</name>
                    <requestFilter>
                        request.addHeader('Authorization', 'auth value')
                    </requestFilter>
                    <stateChangeRequestFilter>
                        request.addHeader('Authorization', 'auth value')
                    </stateChangeRequestFilter>
               <stateChangeUrl>http://mywebsite.com/myservice/myaction
                            </stateChangeUrl>
                            <stateChangeUsesBody>true</stateChangeUsesBody>
                            <protocol>http</protocol>
                            <host>http://localhost</host>
                            <path></path>
                            <port>443</port>
                            <pactBroker>
                                <url>https://mypactbroker</url>
                                <authentication>
                                    <username>my_usrname</username>
                                    <password>my_pwd</password>
                                </authentication>
                            </pactBroker>
                            <insecure>true</insecure>
                        </serviceProvider>
                    </serviceProviders>
</plugin>

my pact file:

{
    "provider": {
        "name": "wProvider"
    },
    "consumer": {
        "name": "tConsumer"
    },
    "interactions": [
        {
            "description": "A new interaction with wProvider",
            "request": {
                "method": "POST",
                "path": "/somePath",
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": ""
            },
            "response": {
                "status": 200,
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": " ",
                "matchingRules": {
                    "body": {
                        // some matching rules
                    }
                }
            },
            "providerStates": [
                {
                    "name": "wProvider will create new stuff"
                }
            ]
        }
    ],
    "metadata": {
        "pactSpecification": {
            "version": "3.0.0"
        },
        "pact-jvm": {
            "version": "3.6.7"
        }
    }
}

When I execute mvn pact:verify -X command I see in console:

Given Wprovider will generate new stuff

[DEBUG] Invoked state change http://mywebsite.com/myservice/myaction -> HTTP/1.1 200 OK [DEBUG] State Change: "ProviderState(name=http://mywebsite.com/myservice/myaction, params={})" -> Ok(value={})

I am missing some settings related to @State with mvn pact:verify?

vasilev21
  • 113
  • 2
  • 4
  • 15

2 Answers2

1

It looks like the @State annotation isn't supported for the Maven plugin (unlike the JUnit setup).

The docs have the following to say about this:

For each provider you can specify a state change URL to use to switch the state of the provider. This URL will receive the providerState description and parameters from the pact file before each interaction via a POST.

So you may need to add a dynamic endpoint (e.g. /_pact/state) that can take the request and setup the given state.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • I agree with this, if maven plugin is used then and endpoint (presumably in the provider application) is needed to handle/take state request. – vasilev21 Jun 12 '19 at 12:52
0

The Maven plugin (as well as the Gradle one) execute the Pact verification against a running provider when you execute pact:verify. The @State method you have defined is for a JUnit test. The Maven plugin does not execute JUnit tests, you run them as part of the test Maven phase which is executed by the Maven Surefire plugin.

The documentation link that Matt provided above will let you know how to execute state changes with pact:verify, but if you have written a JUnit test you don't need to use that.