我试图在提供商端进行验证,但出现错误-
验证DataConsumer和DataProvider之间的协定[使用File pact/DataConsumer DataProvider.json]给定某种状态请求json数据请求失败-对于输入字符串:";\空";
不确定我错过了什么。
我的Pojo-
@EqualsAndHashCode
@RequiredArgsConstructor
@Builder(toBuilder = true)
@JsonDeserialize(builder = DataModel.DataModelBuilder.class)
public class DataModel {
@JsonProperty("name")
private final String name;
@JsonProperty("price")
private final double price;
}
合同-
{
"provider": {
"name": "DataProvider"
},
"consumer": {
"name": "DataConsumer"
},
"interactions": [
{
"description": "a request for json data",
"request": {
"method": "GET",
"path": "/get/ice/2.0"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json; charsetu003dUTF-8"
},
"body": {
"price": 10,
"name": "some name"
},
"matchingRules": {
"header": {
"Content-Type": {
"matchers": [
{
"match": "regex",
"regex": "application/json(;\s?charsetu003d[\w\-]+)?"
}
],
"combine": "AND"
}
}
},
"generators": {
"body": {
"$.name": {
"type": "ProviderState",
"expression": "\${name}",
"dataType": "STRING"
},
"$.price": {
"type": "ProviderState",
"expression": "\${price}",
"dataType": "FLOAT"
}
}
}
},
"providerStates": [
{
"name": "some state"
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "3.6.15"
}
}
}
测试-
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Provider("DataProvider")
@PactFolder(value = "pact")
public class ContractVerificationTest {
@TestTemplate
@ExtendWith(PactVerificationSpringProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@State("some state")
void testPact() {
}
}
代码-
https://github.com/nrworld4/pact-consumer-demo
https://github.com/nrworld4/pact-demo-provider
在提供者测试中,您没有返回提供者状态注释中的值(名称、价格((它目前什么都不做(,因此当Pact试图动态替换请求中的值时,它们为null。
你真的需要它们首先由提供者生成吗?
请参阅https://pactflow.io/blog/injecting-values-from-provider-states/有关如何使用和修复的详细示例。
更新可能是您对参数进行了双重转义吗?
示例中:
.queryParameterFromProviderState("accountNumber", "${accountNumber}", "100")
在您的代码中:
.valueFromProviderState("price", "\${price}", 10.0)