为多家供应商签订合同

时间:2019-05-20 15:05:16

标签: spring-boot pact

我有一个使用其他多个微服务的客户端UI微服务,因此我想为每个提供商编写合同测试。

我尝试编写以下代码:

public class ClientUIContractProductsTest {
    // Pact mock Provider

    @Rule
    public PactProviderRuleMk2 providerOrdersMicroservice = new PactProviderRuleMk2("microservice-orders", "localhost", 9002, this);

    @Rule
    public PactProviderRuleMk2 providerProductsMicroservice = new PactProviderRuleMk2("microservice-products", "localhost", 9005, this);

    // Step (1)
    @Pact(consumer = "microservice-clientui")
    public RequestResponsePact createPactForProducts(PactDslWithProvider builder) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");

        return builder
                .given("test GET /orders")
                    .uponReceiving("GET REQUEST FOR ORDERS")
                    .path("/orders")
                    .method("GET")
                .willRespondWith()
                    .status(200)
                    .headers(headers)
                    .body(LambdaDsl.newJsonArray((a) -> {
                                a.object((o) -> o
                                        .numberType("id", 1)
                                        .numberType("productId", 1)
                                        .timestamp("dateOrder", "yyyy-MM-dd'T'HH:mm:ss.SSS+0000")
                                        .numberType("quantity",1)
                                        .booleanType("orderPayed", false)
                                );
                            }
                    ).build())
                .given("test GET /Products")
                    .uponReceiving("GET REQUEST FOR PRODUCTS")
                    .path("/Products")
                    .method("GET")
                .willRespondWith()
                    .status(200)
                    .headers(headers)
                    .body(PactDslJsonArray.arrayEachLike()
                            .numberType("id", 0)
                            .stringType("title", "Candle working with fire")
                            .stringType("description", "Candle working like a bulb but without electricity")
                            .stringType("image","https://live.staticflickr.com/3408/3279558099_6dc30be4b6_b.jpg")
                            .numberType("price", 22)
                            .closeObject())
                .toPact();
    }

    @Test
    @PactVerification()
    public void pactVerification() {

        // when
        ResponseEntity<String> responseOrders = new RestTemplate()
                .getForEntity(providerOrdersMicroservice.getUrl() + "/orders", String.class);

        // Step (4)
        // then
        assertThat(responseOrders.getStatusCode().value()).isEqualTo(200);

        /*
        *  Pact verification for products micro-service
        * */
        // when
        ResponseEntity<String> responseProducts = new RestTemplate()
                .getForEntity(providerProductsMicroservice.getUrl() + "/Products", String.class);

        // Step (4)
        // then
        assertThat(responseProducts.getStatusCode().value()).isEqualTo(200);

    }

}

但是运行此代码时出现错误。但是当我只跑一个@Rule

如何实施合同以模拟多个提供商?

1 个答案:

答案 0 :(得分:0)

我在这里找到了解决方法:https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-junit#requiring-a-test-with-multiple-providers

代码如下:

package com.clientui;


import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRuleMk2;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslJsonArray;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.RequestResponsePact;
import io.pactfoundation.consumer.dsl.LambdaDsl;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class ClientUIContractProductsTest {
    // Pact mock Provider

    @Rule
    public PactProviderRuleMk2 providerOrdersMicroservice = new PactProviderRuleMk2("microservice-orders",this);

    @Rule
    public PactProviderRuleMk2 providerProductsMicroservice = new PactProviderRuleMk2("microservice-products", this);

    // Step (1)
    @Pact(provider= "microservice-orders", consumer = "microservice-clientui")
    public RequestResponsePact createPactForOrders(PactDslWithProvider builder) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");

        return builder
                .given("test GET /orders")
                    .uponReceiving("GET REQUEST FOR ORDERS")
                    .path("/orders")
                    .method("GET")
                .willRespondWith()
                    .status(200)
                    .headers(headers)
                    .body(LambdaDsl.newJsonArray((a) -> {
                                a.object((o) -> o
                                        .numberType("id", 1)
                                        .numberType("productId", 1)
                                        .timestamp("dateOrder", "yyyy-MM-dd'T'HH:mm:ss.SSS+0000")
                                        .numberType("quantity",1)
                                        .booleanType("orderPayed", false)
                                );
                            }
                    ).build())
                .toPact();
    }

    @Pact(provider= "microservice-products", consumer = "microservice-clientui")
    public RequestResponsePact createPactForProducts(PactDslWithProvider builder) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");

        return builder
                .given("test GET /Products")
                .uponReceiving("GET REQUEST FOR PRODUCTS")
                .path("/Products")
                .method("GET")
                .willRespondWith()
                .status(200)
                .headers(headers)
                .body(PactDslJsonArray.arrayEachLike()
                        .numberType("id", 0)
                        .stringType("title", "Candle working with fire")
                        .stringType("description", "Candle working like a bulb but without electricity")
                        .stringType("image","https://live.staticflickr.com/3408/3279558099_6dc30be4b6_b.jpg")
                        .numberType("price", 22)
                        .closeObject())
                .toPact();
    }

    @Test
    @PactVerification({"microservice-orders", "microservice-products"})
    public void pactVerification() {

        // when
        ResponseEntity<String> responseOrders = new RestTemplate()
                .getForEntity(providerOrdersMicroservice.getUrl() + "/orders", String.class);

        // Step (4)
        // then
        assertThat(responseOrders.getStatusCode().value()).isEqualTo(200);

        /*
        *  Pact verification for products micro-service
        * */
        // when
        ResponseEntity<String> responseProducts = new RestTemplate()
                .getForEntity(providerProductsMicroservice.getUrl() + "/Products", String.class);

        // Step (4)
        // then
        assertThat(responseProducts.getStatusCode().value()).isEqualTo(200);

    }

}