5

我不明白 assert 在@PactVerification. 对我来说,它更像是一种复杂的说法1 == 1。例如:

import static org.assertj.core.api.Assertions.assertThat;
public class PactConsumerDrivenContractUnitTest {
    @Rule
    public PactProviderRuleMk2 mockProvider
      = new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
    @Pact(consumer = "test_consumer")
    public RequestResponsePact createPact(PactDslWithProvider builder) {            
        return builder
          .given("test GET ")
          .uponReceiving("GET REQUEST")
          .path("/")
          .method("GET")
          .willRespondWith()
          .body("{\"condition\": true, \"name\": \"tom\"}")
    }
    @Test
    @PactVerification()
    public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
        //when
        ResponseEntity<String> response
          = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class);
        //then
        assertThat(response.getBody()).contains("condition", "true", "name", "tom");        
    }
}

所以首先在“createPact”中我们声明

body("{\"condition\": true, \"name\": \"tom\"}")

然后在givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody注释中@PactVerification我们这样做

assertThat(response.getBody()).contains("condition", "true", "name", "tom");

但为什么?我们就是这么说的!据我所见,断言没有出现在生成的 Pact 文件中。它的接缝填充没有目的?

除此之外,我认为合同测试的想法是减少对集成测试的需求,因为例如如果测试数据发生变化,它们可能会中断。但是这里我们仍然依赖测试数据。如果 Provider 中没有“Tom”,则测试将失败。我主要想测试合同是否损坏,而不是测试数据是否已更改。

4

2 回答 2

4

给出的例子是一个人为的例子。在使用 Pact 的现实生活中,您不会这样做。您PactVerification将调用一个协作方法/类/事物,该方法负责对您正在模拟的服务的外部调用。

所以你的断言是关于协作功能正在做什么。

例如。用户服务可能会创建具有某些属性的对象,您知道这些属性仅由该外部调用填充。

于 2017-11-28T04:23:50.450 回答
2

在您的测试方法中测试断言@PactVerification不是强制性的,但它仍然可能非常有帮助。例如,您可能在 JSON 正文字符串中打错字,而您将无法在测试中捕捉到它,这将破坏提供者的管道。在这种情况下,断言与生成的 Pact 文件无关,它们扮演着看守的角色,最终检查您刚刚定义的合约 ( RequestResponsePact) 是否符合您的所有期望 ( assertions)。

另外值得一提的是,只有当提供者试图发布一个让你的期望被打破的变化时,你的消费者合同测试才应该被打破。编写好的合约测试是消费者的责任。在您的示例中,您定义了以下期望:

@Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {            
    return builder
        .given("test GET ")
        .uponReceiving("GET REQUEST")
            .path("/")
            .method("GET")
        .willRespondWith()
            .body("{\"condition\": true, \"name\": \"tom\"}")
}

只要condition==truename== ,这个合同就会得到满足tom。这是对响应的过度规范。您可以改为使用 PactDslJsonBody DSL定义更灵活的响应:

@Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    final DslPart body = new PactDslJsonBody()
            .stringType("name", "tom")
            .booleanType("condition", true);

    return builder
            .given("test GET ")
            .uponReceiving("GET REQUEST")
                .path("/")
                .method("GET")
            .willRespondWith()
                .body(body)
            .toPact();
}

该片段将生成 Pact 文件,如:

{
    "provider": {
        "name": "providerA"
    },
    "consumer": {
        "name": "test_consumer"
    },
    "interactions": [
        {
            "description": "GET REQUEST",
            "request": {
                "method": "GET",
                "path": "/"
            },
            "response": {
                "status": 200,
                "headers": {
                    "Content-Type": "application/json; charset=UTF-8"
                },
                "body": {
                    "condition": true,
                    "name": "tom"
                },
                "matchingRules": {
                    "body": {
                        "$.name": {
                            "matchers": [
                                {
                                    "match": "type"
                                }
                            ],
                            "combine": "AND"
                        },
                        "$.condition": {
                            "matchers": [
                                {
                                    "match": "type"
                                }
                            ],
                            "combine": "AND"
                        }
                    }
                }
            },
            "providerStates": [
                {
                    "name": "test GET "
                }
            ]
        }
    ],
    "metadata": {
        "pact-specification": {
            "version": "3.0.0"
        },
        "pact-jvm": {
            "version": "3.5.10"
        }
    }
}

主要区别在于此 Pact 文件用于matchingRules测试是否:

  • condition字段类型是boolean
  • name字段类型是String

对于字符串,PactDslJsonBody.stringMatcher(name, regex, value)如果需要,您还可以使用方法。它允许您定义将使用当前字段值进行测试的正则表达式。

于 2017-11-28T08:11:55.700 回答