0

I have defined some streams producers (@Output) and consumers (@Input) in my spring boot application as long as my rest endpoints. Now I want to test both REST + Streams using

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

First of all I want to know if this is possible. For REST I'm auto wiring the TestRestTemplate and everything goes fine:

@Autowired
private TestRestTemplate restTemplate;

But for streams I'm trying to use:

@ClassRule
public static RabbitTestSupport rabbitTestSupport = new RabbitTestSupport();

Which enables me to recognise if Rabbit is up or not, and this is working fine, but then when I try to

 @Autowired
private MessageChannel myProducer;

and send messages, I don't get any error, but the messages are not consumed by my app.

I got the feeling that both producer and consumer are started as part of my app instead of in a decoupled context and for that reason this is not working.

Producers and consumers are working fine in separate apps, so it seems something related with the test configuration.

Any ideas? did someone manage to test both REST and Streams in the same test using @SpringBootTest because I couldn't find any reference.

I'm adding a reproducer here: https://github.com/Salaboy/test-spring-cloud-streams/

Some pointers: https://github.com/Salaboy/test-spring-cloud-streams/blob/master/src/main/java/org/salaboy/streams/SampleApplication.java#L40

and the test: https://github.com/Salaboy/test-spring-cloud-streams/blob/master/src/test/java/org/salaboy/streams/MyAppStreamsTest.java

Which uses these properties: https://github.com/Salaboy/test-spring-cloud-streams/blob/master/src/test/resources/test-application.properties

I will appreciate any help that you can provide.

salaboy
  • 4,123
  • 1
  • 14
  • 15
  • SOLVED: the main problem was the test-binder added to the class path it was overlapping with the rabbit mq provider which causes the messages to never arrive to the running application. The test in my repository is showing the correct working of these components and I will probably write a blog post about this, because I couldn't find any example of this anywhere. – salaboy Jul 19 '17 at 07:36

2 Answers2

0

Can you explain a little bit more messages are not consumed by my apps. I'm asking because it's not clear how you are consuming messages, is your app listening or a http endpoint and then sending data via the broker using a Source output? and then you have a Sink input to consume them?

Vinicius Carvalho
  • 3,994
  • 4
  • 23
  • 29
  • Yes exactly. So I have a spring-boot app with @Input(HISTORY_CONSUMER) SubscribableChannel historyConsumer(); and the EnableBinding() annotation correctly set up. Because when I send messages from a different app, messages are consumed. But when I send messages via my test using @SpringBootTest and creating a new @Output(HISTORY_PRODUCER) MessageChannel historyProducer(); in the tests, this doesn't seem to work. Do you happen to have a test showing this interaction? I'm assuming that this is something that everybody will want to do – salaboy Jul 16 '17 at 21:52
  • I think I know what's going on. Your channels have distinct names. So your producer messages are being sent to a different queue then your consuming subscriber. You can try to override them using `spring.cloud.stream.bindings.historyProducer.destination=sharedName` `spring.cloud.stream.bindings.historyConsumer.destination=sharedName` this way both would be bound to the same destination. – Vinicius Carvalho Jul 17 '17 at 00:18
  • I have the correct configuration for those channels. I'm already producing that, but I think that the consumer and producer configuration are being mixed up. I have an application.properties inside my app, and then I need to add another application.properties for my test for the test producer. I will try to create a reproducer and post the link here – salaboy Jul 17 '17 at 07:45
  • I've added a reproducer here: https://github.com/Salaboy/test-spring-cloud-streams/ You should be able to see the issue in less than 2 minutes with that project. Let me know if you need more clarification. I appreciate the help – salaboy Jul 17 '17 at 09:22
  • I'm cloning the repo, but the first thing that jumped on is the dependency on boot 2.0. Can you revert back to boot 1.5.4 and Ditmars 1.3 for Stream? There may be issues still with boot 2.0, and I want to keep the number of variables reduced when looking into this. One of the main things we are having issues with boot 2.0 is around environment and property resolution, so I'd just take a step back for the moment. – Vinicius Carvalho Jul 17 '17 at 13:35
  • Hi Vinicius, thanks for the help, but this needs to work on spring boot 2 or we need to report it to the guys working on the milestones so they can fix it. It is imperative to get it working with that version. Do you have any idea on how to report this to the spring team? – salaboy Jul 17 '17 at 22:09
  • Vinicius, I also think that if you revert that back to 1.5.4 and get it working, it is a clear indication that there is something broken in spring boot 2 and now it is a good time to fix it. Does the test make sense at least? – salaboy Jul 18 '17 at 08:00
  • For the record: We closed the issue, there was nothing wrong with beinbg boot 2.0, instead it's the TestBinder being instantiated instead of the RabbitBinder on user's configuration – Vinicius Carvalho Jul 19 '17 at 10:52
  • thanks a lot for helping me to find that out. Now the linked repository contain an example of how to test streams and rest endpoints in the same test. – salaboy Jul 25 '17 at 15:19
0

I use this library to easily tests spring cloud messages in my app with cucumber/gherkin: https://github.com/RedFroggy/spring-cucumber-messaging

Michael Desigaud
  • 2,115
  • 1
  • 15
  • 15