4

我正在尝试生成 Pact 文件。测试在 Eclipse 中的“Run As-Junit Test”时通过。但是,无法真正理解为什么没有生成 Pact 合同文件。你能帮忙吗?下面是我的测试代码:

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());
      }

}

我的 POM 文件如下:

<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>
4

3 回答 3

1

我遇到了同样的问题,我的 Pact 文件没有在我在 maven-surfire-plugin 中指定的文件夹中生成。

但是我意识到文件已经生成,但是在文件夹“目标>契约”中。要生成文件,我只需要执行 maven 目标“mvn test”。

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

我已经像这样设置了 pact.rootDir,但它总是在 target/pact 文件夹中生成 pact 合同,所以现在我从那里获取它。

于 2019-08-28T09:37:34.293 回答
0

您似乎缺少createFragment函数中的状态规范。

要在您的示例中修复它,请将您的函数更改为:

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

从这里,验证过程将知道根据名称检查该状态,验证它,然后将 Pact 文件写入pacts目录下,这是默认设置。

这是一个对我有用的例子:

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());
    }
}
于 2017-04-28T00:20:38.403 回答
0

Pact JVM 似乎忽略了 buildDirectory 属性

尝试以下配置:

        <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 插件配置

于 2018-12-06T13:56:44.753 回答