简体   繁体   中英

Spring Cloud Contract with Spring AMQP

So I've been trying to use Spring Cloud Contract to test 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. Another thing is "outputMessage", where we can verify whether the method call before have correctly resulting on some message body sent to certain exchange.

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:

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.

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.

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. It will then capture the message and routing key on your RabbitTemplate call to "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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