简体   繁体   English

与Spring AMQP的Spring Cloud合同

[英]Spring Cloud Contract with Spring AMQP

So I've been trying to use Spring Cloud Contract to test RabbitListener. 所以我一直在尝试使用Spring Cloud Contract测试RabbitListener。

So far I have found out that by defining "triggeredBy" in contract, the generated test will call the method provided there and so we will need to provide the actual implementation of what that method do in the TestBase. 到目前为止,我发现通过在合同中定义“ triggeredBy”,生成的测试将调用那里提供的方法,因此我们需要提供该方法在TestBase中的实际实现。 Another thing is "outputMessage", where we can verify whether the method call before have correctly resulting on some message body sent to certain exchange. 另一件事是“ outputMessage”,在这里我们可以验证方法调用之前是否已经正确地将某些消息主体发送到了某些交换机。

Source: documentation and sample 来源: 文档样本

My question is, is there any way to produce the input message from the contract, instead of triggering own custom method? 我的问题是,有什么办法可以从合同中产生输入消息,而不是触发自己的自定义方法? Perhaps something similar like Spring Integration or Spring Cloud Stream example in the documentation: 也许类似文档中的Spring Integration或Spring Cloud Stream示例之类的东西:

Contract.make {
    name("Book Success")
    label("book_success")
    input {
        messageFrom 'input.exchange.and.maybe.route'
        messageHeaders {
            header('contentType': 'application/json')
            header('otherMessageHeader': '1')
        }
        messageBody ([
                bookData: someData
        ])
    }
    outputMessage {
        sentTo 'output.exchange.and.maybe.route'
        headers {
            header('contentType': 'application/json')
            header('otherMessageHeader': '2')
        }
        body([
                bookResult: true
        ])
    }
}

I couldn't find any examples in their sample project that show how to do this. 我在他们的示例项目中找不到任何示例来说明如何执行此操作。

Having used spring cloud contract to document and test rest api services, if possible I would like to stay consistent by defining both the input and expected output in contract files for event based services. 使用Spring Cloud Contract来记录和测试其余api服务后,如果可能的话,我想通过在基于事件的服务的合约文件中定义输入和预期输出来保持一致。

Never mind, actually its already supported. 没关系,实际上它已经受支持了。 For unknown reason the documentation in "Stub Runner Spring AMQP" does not list the scenario like others previous sample. 出于未知原因, “ Stub Runner Spring AMQP”中的文档没有像其他先前的示例一样列出该方案。

Here is how I make it works: 这是我的工作方式:

    Contract.make {
        name("Amqp Contract")
        label("amqp_contract")
        input {
            messageFrom 'my.exchange'
            messageHeaders {
                header('contentType': 'text/plain')
                header('amqp_receivedRoutingKey' : 'my.routing.key')
            }
            messageBody(file('request.json'))
        }
        outputMessage {
            sentTo 'your.exchange'
            headers {
                header('contentType': 'text/plain')
                header('amqp_receivedRoutingKey' : 'your.routing.key')
            }
            body(file('response.json'))
        }
    }

This will create a test that will call your listener based on "my.exchange" and "my.routing.key" triggering the handler method. 这将创建一个测试,该测试将基于触发处理程序方法的“ my.exchange”和“ my.routing.key”调用您的侦听器。 It will then capture the message and routing key on your RabbitTemplate call to "your.exchange". 然后它将在RabbitTemplate调用“ your.exchange”上捕获消息和路由键。

    verify(this.rabbitTemplate, atLeastOnce()).send(eq(destination), routingKeyCaptor.capture(),
            messageCaptor.capture(), any(CorrelationData.class));

Both message and routing key then will be asserted. 然后将声明消息和路由密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM