Java 尽管测试通过,但未生成Pact文件
我正在尝试生成Pact文件。当Eclipse中的“作为Junit测试运行”时,测试通过。但是,无法真正理解为什么不生成Pact合同文件。你能帮忙吗?下面是我的测试代码:Java 尽管测试通过,但未生成Pact文件,java,eclipse,junit,pact,Java,Eclipse,Junit,Pact,我正在尝试生成Pact文件。当Eclipse中的“作为Junit测试运行”时,测试通过。但是,无法真正理解为什么不生成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.d
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.0.0
消费者
消费者
0.0.1-快照
org.apache.maven
maven插件api
3.3.9
org.apache.maven.reporting
maven报告程序
2.2
commons cli
commons cli
1.4
org.apache.httpcomponents
流畅的hc
4.5.2
org.codehaus.plexus
尾丛
3.0.24
朱尼特
朱尼特
4.8
au.com.dius
pact-jvm-consumer-junit_2.10
2.4.18
org.codehaus.groovy
groovy all
2.4.7
回写
回归经典
1.0.13
org.slf4j
slf4j api
1.7.25
org.apache.maven.plugins
maven surefire插件
2.18
PPP/pact
${project.build.directory}
在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());
}
}
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>
org.apache.maven.plugins
我遇到了同样的问题,我的Pact文件没有在我在maven冲浪插件中指定的文件夹中生成
然而,我意识到文件已经生成,但是在文件夹“target>pacts”中。为了生成文件,我只需要执行maven目标“mvn测试”
maven-surefire插件
src/测试/资源/合同
${project.build.directory}
我已经像这样设置了pact.rootDir,但它总是在target/pact文件夹中生成pact合同,因此现在我从那里获取它。我正在尝试在我的组织中引入pact。任何帮助都将不胜感激。我尝试了很多东西,但仍然无法解决这个问题。您好,但是如果我包含state,它将显示为已弃用。即使我添加state,也不会生成pact文件。请帮忙:'(这是一个Maven项目,我相信pact文件是在Maven->install或build命令后生成的?构建成功,但没有生成pact文件。嗯,这很奇怪。你认为你可以用你的Maven设置更新你的问题吗?这样,我可以测试你在本地做什么,并找出发生了什么。嗨,我添加了pom、 xml。如果您需要更多详细信息,请告诉我。非常感谢您的帮助!这就是它对我有效的原因。对于那些使用maven项目运行eclipse的人,运行junit测试将不会生成pact文件。必须运行mvn测试才能生成它
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<pact.rootDir>src/test/resources/pacts</pact.rootDir>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>