当前位置: 首页 > 知识库问答 >
问题:

Pact提供程序验证失败:对于输入字符串:“\null”

游鸣
2023-03-14

不知道我错过了什么。

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

共有1个答案

戴浩初
2023-03-14

您没有在提供程序测试中从提供程序状态注释返回值(名称、价格)(当前没有执行任何操作),因此当Pact尝试在请求中动态替换值时,这些值是空的。

您实际上需要它们首先由提供者生成吗?

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

.queryParameterFromProviderState("accountNumber", "\${accountNumber}", "100")
.valueFromProviderState("price", "\\${price}", 10.0)
 类似资料: