Spring 与 Spring AMQP 的云合同

Spring Cloud Contract with Spring AMQP

所以我一直在尝试使用 Spring Cloud Contract 来测试 RabbitListener。

到目前为止,我发现通过在合同中定义 "triggeredBy",生成的测试将调用那里提供的方法,因此我们需要提供该方法在 TestBase 中执行的操作的实际实现。 另一件事是 "outputMessage",我们可以在其中验证之前的方法调用是否正确地导致某些消息正文发送到特定交换。

来源:documentation and sample

我的问题是,有没有办法从合约中生成输入消息,而不是触发自己的自定义方法? 也许类似于文档中的 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
        ])
    }
}

我在他们的示例项目中找不到任何说明如何执行此操作的示例。

使用 spring 云合同记录和测试其余 api 服务后,如果可能的话,我想通过在合同文件中为基于事件的服务定义输入和预期输出来保持一致。

没关系,其实已经支持了。 由于未知原因,"Stub Runner Spring AMQP" 中的文档没有像其他先前示例那样列出场景。

以下是我的工作原理:

    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'))
        }
    }

这将创建一个测试,该测试将根据 "my.exchange" 和 "my.routing.key" 触发处理程序方法来调用您的侦听器。 然后它将在您对 "your.exchange".

的 RabbitTemplate 调用中捕获消息和路由密钥
    verify(this.rabbitTemplate, atLeastOnce()).send(eq(destination), routingKeyCaptor.capture(),
            messageCaptor.capture(), any(CorrelationData.class));

然后消息和路由密钥都将被断言。