4

I am trying to generate Pact file.The test is passing when is "Run As-Junit Test" in Eclipse. However, unable to really understand why the Pact Contract File is not generated. Can you please help? Below is my test code:

package pact;
import au.com.dius.pact.consumer.*;
import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.model.PactFragment;
import org.junit.Assert;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.dsl.PactDslWithState;

import org.junit.Rule;
import org.junit.Test;
import utils.Configuration;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class GetHelloWorldTest
{

    @Rule
    public PactProviderRule rule = new PactProviderRule("PP Provider", "localhost", 9000, this);
     private String helloWorldResults;

    @Pact(provider = Configuration.DUMMY_PROVIDER,consumer = Configuration.DUMMY_CONSUMER)
    public PactFragment createFragment(PactDslWithProvider builder)//TODO 
    {

        return builder
                .uponReceiving("get hello world response")
                .path("/hello-world")
                .method("GET")
                .willRespondWith()
                .status(200)
                .body("{\"id\":2,\"content\":\"Hello, Stranger!\"}")
                .toFragment();
    }

    @Test
    @PactVerification(value = "PP provider")
    public void shouldGetHelloWorld() throws IOException
    {
        DummyConsumer restClient = new DummyConsumer(Configuration.SERVICE_URL);
        Assert.assertEquals("{\"id\":32,\"content\":\"Hello, Stranger!\"}",restClient.getHelloWorld());
      }

}

My POM File is as below :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>consumer</groupId>
  <artifactId>consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>

 <dependencies>


  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.3.9</version>
</dependency>


 <dependency>
    <groupId>org.apache.maven.reporting</groupId>
    <artifactId>maven-reporting-impl</artifactId>
    <version>2.2</version>
</dependency>

<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.4</version>
</dependency>


<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>fluent-hc</artifactId>
    <version>4.5.2</version>
</dependency>

<dependency>
    <groupId>org.codehaus.plexus</groupId>
    <artifactId>plexus-utils</artifactId>
    <version>3.0.24</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8</version>
</dependency>

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-consumer-junit_2.10</artifactId>
    <version>2.4.18</version>
</dependency>


<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.7</version>
</dependency>
<dependency> 
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.0.13</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>


</dependencies>
 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18</version>
        <configuration>
          <systemPropertyVariables>
            <pact.rootDir>PPPPP/pact</pact.rootDir>
            <buildDirectory>${project.build.directory}</buildDirectory>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>



</project>
PaChSu
  • 297
  • 3
  • 13
  • I am trying to introduce Pact in my organisation. Any help would be really appreciated. I tried a lot of things I am still stuck on this. – PaChSu Apr 27 '17 at 08:49

3 Answers3

1

I had the same problem that my Pact file has not been generated in the folder that I specified in the maven-surfire-plugin.

However I realized that the file has been generated, but in the folder "target > pacts". To generate the file I just needed to execute the maven goal "mvn test".

<artifactId>maven-surefire-plugin</artifactId>
<configuration>
    <systemPropertyVariables>
        <pact.rootDir>src/test/resources/pacts</pact.rootDir>
        <buildDirectory>${project.build.directory}</buildDirectory>
    </systemPropertyVariables>

I have set the pact.rootDir like this but it always generates the pact contract in the target/pact folder so now I am taking it from there.

Invest
  • 545
  • 6
  • 9
  • this is the reason it worked for me. For those running eclipse with maven projects, running junit tests will not generate the pact files. mvn test must be run to generate it – Sajith Dilshan Jamal Sep 24 '19 at 21:46
0

It would seem that you're missing your state specification in the createFragment function.

To fix it in your example, change your function to this:

@Pact(state = "PP provider", provider = Configuration.DUMMY_PROVIDER,consumer = Configuration.DUMMY_CONSUMER)
public PactFragment createFragment(PactDslWithProvider builder)

From here, the verification process will know to check for that state because of the name, verify it, then write the Pact file out under the pacts directory, which is the default.

Here's an example that works for me:

package pact;

import au.com.dius.pact.consumer.*;
import au.com.dius.pact.model.PactFragment;
import org.junit.Rule;
import org.junit.Test;
import utils.Configuration;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class GetHelloWorldTest
{
    @Rule
    public PactRule rule = new PactRule(Configuration.MOCK_HOST, Configuration.MOCK_HOST_PORT, this);
    private DslPart helloWorldResults;

    @Pact(state = "HELLO WORLD", provider = Configuration.DUMMY_PROVIDER, consumer = Configuration.DUMMY_CONSUMER)
    public PactFragment createFragment(ConsumerPactBuilder.PactDslWithProvider.PactDslWithState builder)
    {
        helloWorldResults = new PactDslJsonBody()
                .id()
                .stringType("content")
                .asBody();

        return builder
                .uponReceiving("get hello world response")
                .path("/hello-world")
                .method("GET")
                .willRespondWith()
                .status(200)
                .headers(Configuration.getHeaders())
                .body(helloWorldResults)
                .toFragment();
    }

    @Test
    @PactVerification("HELLO WORLD")
    public void shouldGetHelloWorld() throws IOException
    {
        DummyConsumer restClient = new DummyConsumer(Configuration.SERVICE_URL);
        assertEquals(helloWorldResults.toString(), restClient.getHelloWorld());
    }
}
J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • Hi , but if I include state, it is showing as deprecated. – PaChSu Apr 28 '17 at 02:03
  • Even if I add the state, pact file is still not generated. Please help :'( – PaChSu Apr 28 '17 at 02:09
  • This is a Maven project, and I believe the pact file is generated after the Maven->install or build command? The build is a success but no pact file is generated. – PaChSu Apr 28 '17 at 02:23
  • Hm, that's weird. Do you think you can update your question with your maven setup? This way, I can test what you're doing locally and figure out what's going on. – J_A_X Apr 28 '17 at 05:01
  • Hi, I have added the pom.xml. Let me know if you require any more details. Thanks a lot for your help! – PaChSu May 04 '17 at 02:25
0

Pact JVM appears to ignore the buildDirectory property

Try the following configuration:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18</version>
            <configuration>
                <systemPropertyVariables>
                    <pact.rootDir>${project.build.directory}/pacts</pact.rootDir>
                    <buildDirectory>${project.build.directory}</buildDirectory>
                </systemPropertyVariables>
            </configuration>
        </plugin>

maven surefire plugin configuration

Gapmeister66
  • 846
  • 9
  • 14