Open srs-haw opened 6 years ago
If I understand correctly, you would like to verify that a method was executed on a channel?
Why not write a bean post processor that will wrap your channel with Mockito.spy(...)
? That way you will be able to assert whatever you need.
No, the main problem is, that a (rabbitmq)Channel-instance musst be passed to the handle-method above somehow - otherwise an exception is thrown:
org.springframework.messaging.MessageHandlingException: Missing header 'amqp_channel' for
method parameter type [interface com.rabbitmq.client.Channel], failedMessage=GenericMessage...
This instance must somehow be passed from contract tests. In my own tests I can do something like this:
@Test
public void messagingTest() throws Exception {
messagingChannels.eventsBookingserviceIn()
.send(MessageBuilder.withPayload(7)
.setHeader(AmqpHeaders.CHANNEL, mock(Channel.class))
.setHeader(AmqpHeaders.DELIVERY_TAG, 1)
.build());
...
Ahhh cause the header value must be an object...
AFAIR if you used Groovy to define the contract, you could be able to provide the pair of client / server via $(consumer("foo"), producer(execute("getMockChannel()")))
. I think that would work. In YAML however we don't support such things to define the input
message. That might be a missing feature.
Can you try to write the contract in Groovy in the way I mentioned to see if that "workaround" works for you?
Yes, workaround works! Thanks!
And yes, it would be great to have this in yaml!
Awesome! From my perspective, if there's a workaround, then at least you're not blocked and you can work normally :)
BTW can you confirm that it also works on the consumer side?
I'll check this soon...
Works!
I'd like to propose an enhancement. I have to use manual acknowledgement of messages in a Spring Cloud Stream scenario like this:
Therefore I have to pass a (mock)-Channel instance in my tests - by setting the message headers accordingly. For my own unit tests, this is not an issue.
Unfortunately I didn't find a way to do this in Spring Cloud Contract tests. A solution would be to allow dynamic properties in the messageHeaders-section like in this example this:
getMockChannel() would a be function in my message test base class that just returns a mock(Channel.class) instance. But in Spring Cloud Contract this currently gets compiled to: