0

I'm trying to do a simple spring-cloud-stream unit test to verify the wiring between streams; basically that a handler can read from one stream and write to another. This part is working fine. The problem is that other parts of the app are being started as well; namely, a rabbitMQ listener. There is a method in another class (besides the one I'm testing) which has @RabbitListener. This is the method being called. And I do have rabbit running locally on my machine, for local dev testing. But I don't want this invoked within the test scope.

The spring-cloud-stream test docs here example has

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

I think it's the @SpringBootTest which is starting up the entire configuration, including the RabbitMQ listeners. I've removed the webEnvironment parameter, but that didn't make a difference.

For now, the workaround is to put spring.rabbitmq.listener.simple.auto-startup: false in application.yml, but that's not something I want to continue for various reasons, one of which is that I probably want to unit test that rabbitlistener at one point, albeit in a properly-limited test context.

We're using version 2.0.1 of spring-cloud-stream and spring-cloud-stream-test-support, although this seems to be a more fundamental spring configuration issue that I don't understand how to limit the context.

user26270
  • 6,904
  • 13
  • 62
  • 94

1 Answers1

1

You can use a property placeholder in the autoStartup property

spring.rabbitmq.listener.simple.auto-startup: ${auto.start:true}

and then use a @TestPropertySource in the test case to set it to false.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks; So how would I turn it on again, in another test where I want it on? Is that with @Value? – user26270 Jan 10 '19 at 18:26
  • Omit the property from the `@TestPropertySource` in that class (or set it to true); or you can `@Autowired` the `RabbitListenerEndpointRegistry` and call `registry.getListenerContainer(id).start()` from the test, where `id` is the `id` property on the `@RabbitListener`. – Gary Russell Jan 10 '19 at 18:30