当我从IDE执行提供程序测试时,会执行@State方法,但是mvn pact:verify命令不会执行提供程序的@State
提供程序测试类:
@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")
}
}
我的pom.xml文件:
<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>
我的契约文件:
{
"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"
}
}
}
当我执行mvn pact:verify -X命令时,我会在控制台中看到:
鉴于Wprovider将产生新的东西
[DEBUG]调用状态更改http://mywebsite.com/myservice/myaction -> HTTP / 1.1 200 OK [DEBUG]状态更改:“ ProviderState(name = {http://mywebsite.com/myservice/myaction, params = {})“->确定(值= {})
我缺少一些与mvn pact:verify的@State相关的设置?
答案 0 :(得分:1)
Maven插件似乎不支持@State
注释(与JUnit设置不同)。
docs对此有以下说法:
对于每个提供程序,您都可以指定状态更改URL,以用于切换提供程序的状态。在每次通过POST进行交互之前,该URL将从契约文件中接收providerState描述和参数。
因此,您可能需要添加一个动态端点(例如/_pact/state
)来接受请求并设置给定状态。
答案 1 :(得分:0)
当您执行pact:verify
时,Maven插件(以及Gradle插件)将对正在运行的提供程序执行Pact验证。您定义的@State
方法用于JUnit测试。 Maven插件不执行JUnit测试,您可以在Maven Surefire插件执行的test
Maven阶段中运行它们。
Matt上面提供的文档链接将使您知道如何使用pact:verify
执行状态更改,但是如果您编写了JUnit测试,则不需要使用它。