spring-boot - 为多家供应商写一份合同
问题描述
我有一个使用其他几个微服务的客户端 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
如何实施合同来模拟多个提供者?
解决方案
这是代码:
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);
}
}
推荐阅读
- pytorch - 将 TorchScript 模型导出到 ONNX 时出错 - “RuntimeError:模块包含重叠的属性值”
- javascript - 如何以编程方式 js 制作可迭代的 Promise?
- python - Discord.py:当我试图让我的机器人在命令中赋予角色时,我得到一个 AttributeError
- html - 我可以重复表格标题并将页面分成列吗?
- python - 涉及 `format()` 点符号时的 Python 格式化
- reactjs - 边框颜色在 Tailwind CSS 中不起作用
- javascript - 使用 axios 使用客户端凭据获取 Keycloak access_token 在 NextJS 应用程序中引发 400 错误
- html - 如何从IE页面复制文本并将其粘贴到excel中
- project-reactor - 为助焊剂使用者注册不同的线程
- excel - Excel Sumifs 多个标准