0

我正在尝试在提供者方面进行验证,但出现错误 -

验证 DataConsumer 和 DataProvider 之间的协议 [使用文件 pact/DataConsumer-DataProvider.json] 给定某些状态,请求 json 数据请求失败 - 对于输入字符串:“\null”

不知道我在这里错过了什么。

我的宝乔——

@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; charset\u003dUTF-8"
        },
        "body": {
          "price": 10,
          "name": "some name"
        },
        "matchingRules": {
          "header": {
            "Content-Type": {
              "matchers": [
                {
                  "match": "regex",
                  "regex": "application/json(;\\s?charset\u003d[\\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

4

1 回答 1

0

您没有在提供者测试中从提供者状态注释中返回值(名称、价格)(它目前什么都不做),因此当 Pact 尝试在请求中动态替换这些值时,它们为空。

你真的需要它们首先由提供者生成吗?

有关如何使用和修复的详细示例,请参阅 https://pactflow.io/blog/injecting-values-from-provider-states/ 。

更新 可能是您双重转义参数吗?

在示例中:

.queryParameterFromProviderState("accountNumber", "\${accountNumber}", "100")

在您的代码中:

.valueFromProviderState("price", "\\${price}", 10.0)
于 2020-09-26T21:39:48.420 回答