MessageQueue的契约:如果有MessageQueues,则示例提供程序测试

编程入门 行业动态 更新时间:2024-10-10 04:27:40
本文介绍了MessageQueue的契约:如果有MessageQueues,则示例提供程序测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已通过参考pact仓库中给出的示例示例为MessageProvider编写了示例Pact测试。以下是消费者测试,该测试正在为Provider期望的消息生成PACT json文件。

I have written sample Pact test for MessageProvider by referring to sample example given in pact repo. Following is the consumer test which is generating the PACT json file for the message expected from Provider.

对于API,要验证PACT,我可以使用 pact-jvm-provider-maven插件来执行此操作。在这种情况下,将根据提供商的 actual 托管API服务对PACT进行验证。

In case of API, to verify the PACT, I was able to do this using "pact-jvm-provider-maven" plugin. In this case the PACT is verified against the actual hosted API service of Provider.

我的问题是,在MessageQueue中,如何验证PACT?是否会创建模拟队列?或我需要将一条消息发布到实际的Queue,并需要对照发布到Queue的该消息验证PACT消息。

My Question is, how in case of MessageQueue, PACT will be verified? Will a mock Queue gets created? or I need to publish a message to actual Queue and need to verify the PACT message against this message published to Queue.

有人可以解释它的工作原理吗?

Can someone explain how exactly it works?

还请指出要在提供者端编写的示例代码(示例测试),以验证MessageQueue的消息。

Also please point me to sample code (example test) to be written at provider end to verify the message of MessageQueue.

示例消息(消费者)测试:

Sample message (Consumer) test :

公共类Inbound_Receiving_OpenMessageTest {专用字节[]接收OpenLoadDetailsMessage ;

public class Inbound_Receiving_OpenMessageTest { private byte[] receivingOpenLoadDetailsMessage;

@Rule public MessagePactProviderRule mockProvider = new MessagePactProviderRule(this); @Pact(provider = Configuration.ReceivingProviderOpen, consumer = Configuration.InboundConsumer) public MessagePact createPact(MessagePactBuilder builder) { PactDslJsonBody body = (PactDslJsonBody) new PactDslJsonBody() .stringType("_id") .object("delivery") .stringType("deliveryNumber") .closeObject() .array("state") .object() .stringType("changeTime") .stringValue("status", "OPEN") .stringType("changeUser") .closeObject() .closeArray(); Map<String, String> metadata = new HashMap<String, String>(); metadata.put("contentType", "application/json"); return builder .given("Receiving(Open) App State") .expectsToReceive("Receiving Open Load details Test") .withMetadata(metadata) .withContent(body) .toPact(); } @Test @PactVerification({Configuration.ReceivingProviderOpen, "Receiving(Open) App State"}) public void test() throws Exception { Assert.assertNotNull(new String(receivingOpenLoadDetailsMessage)); LoadDetails openLoadDetails = null; Gson gson = new GsonBuilder().create(); String entity = new String(receivingOpenLoadDetailsMessage); openLoadDetails = gson.fromJson(entity, LoadDetails.class); if(openLoadDetails.getDelivery().getDeliveryNumber() == null || openLoadDetails.getState().get(0).getChangeUser() == null || openLoadDetails.getState().get(0).getChangeTime() == null || openLoadDetails.getState().get(0).getStatus() == null){ Assert.fail("Either one of the field 'deliveryNumber' or 'changeTime' or 'status' or 'changeUser' is NULL"); } } public void setMessage(byte[] messageContents) { receivingOpenLoadDetailsMessage = messageContents; }

}

推荐答案

此博客文章对其进行了详细说明。

This blog post explains it in more detail.

从本质上讲,其想法是,如果您可以验证将消息放入队列的代码是否符合合同(提供者),并且处理来自队列的消息的代码也符合合同(消费者),您实际上不需要消息队列来验证合同。

Essentially, the idea is that if you can verify that the code that puts the message onto the queue conforms to the contract (the provider), and the code that handles the message from the queue also conforms to the contract (the consumer), you don't actually need a message queue to verify the contract.

更多推荐

MessageQueue的契约:如果有MessageQueues,则示例提供程序测试

本文发布于:2023-10-30 18:49:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   契约   测试   程序   MessageQueue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!