Related to #3587 and possibly to spring.rabbitmq.dynamic=false.

I'd like to run tests that must not use the local RabbitMQ server. The autoconfiguration creates a RabbitTemplate bean which automatically connects to my local RabbitMQ server and, when used, sends messages into the system.

I tried setting spring.rabbitmq.listener.auto-startup=false (see #3587), but this does not help. Setting spring.rabbitmq.host= does not seem to do anything, and any (other) invalid option causes a timeout.

A workaround is to create a (mock) bean of type RabbitTemplate. However, this would be my only explicit reference to RabbitMQ from my code, as I'm using AMQP interfaces everywhere.

Could you please add an option to disable RabbitMQ/RabbitTemplate via configuration?

I can already disable AmqpTemplate autocreation using spring.rabbitmq.dynamic=false. Similarly (with better names imho), I can already disable Eureka (eureka.client.enabled=false) and Spring Cloud Bus (spring.cloud.bus.enabled=false).

Comment From: wilkinsona

Could you please add an option to disable RabbitMQ/RabbitTemplate via configuration?

We already have such an option. You can disable any auto-configuration class using the spring.autoconfigure.exclude property. In this case it would be:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration

Does that meet your needs?

Comment From: C-Otto

Thank you very much! Yes, it helps - also with other issues.

Comment From: abelsromero

With v. 1.5.2-RELEASE this is not working.

I have a bean to setup the context of a test like this:

@SpringBootApplication(scanBasePackages = {
    "my.pacakge1",
    "my.pacakge2"
}, exclude = {
    RabbitAutoConfiguration.class,
})
@Configuration
public class TasksTestContext {

It does not work if I use @EnableAutoConfiguration(exclude = {RabbitAutoConfiguration.class}) either.

No matter what I get a RabbitTemplate created.

Comment From: wilkinsona

The suggestion above was to use the spring.autoconfigure.exclude property. You haven't done that.

Comment From: abelsromero

I wrote quickly and the text probably seems rude. It was not my intention. I have used the property and it works, but shouldn't both options work? I suspect it may have something to do with the JUnit runner and the order in which classses are loaded. So, does this mean the annotation option not supported in tests?

Again, apologies if it sounded rude.

Comment From: wilkinsona

No, not rude, it was just a bit strange as the "this" that you showed was different to the "this" that was discussed previously in the issue.

I suspect your problem may be that you have @EnableAutoConfiguration (or @SpringBootApplication) being picked up twice or more by your tests. The exclude isn't cumulative so if one excludes RabbitAutoConfiguration and another does not then it won't be excluded.

Comment From: teosarca

hi,

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration

... seems to not be very helpful in case you have @RabbitListener annotated methods. In that case I get:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'rabbitListenerContainerFactory' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition_jrOrig(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar.resolveContainerFactory(RabbitListenerEndpointRegistrar.java:150)
    at org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar.registerAllEndpoints(RabbitListenerEndpointRegistrar.java:135)
    at org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar.afterPropertiesSet(RabbitListenerEndpointRegistrar.java:128)
    at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.afterSingletonsInstantiated(RabbitListenerAnnotationBeanPostProcessor.java:256)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781)

(spring-rabbit-1.7.2.RELEASE)

Comment From: challengezhou

@teosarca Is this can be resolved by adding@Profile("...") annotation to the class that contains @RabbitListener method? As long as you don't use other beans that depend on ConnectionFactory elsewhere in your code like AmqpTemplate ...

Comment From: mararn1618

I had a slightly different scenario, where I wanted to: - Disable RabbitMQ - Keep autowired fields in existing classes - Didn't want to use @Profile(..) to minimize reengineering costs

I have used what was suggested in https://stackoverflow.com/a/59824234/5244937 instead:

@Value("${de.dpt.app.shopify.enable_rabbitmq_webhooks}")
private Boolean useRabbitMQ;

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setAutoStartup(this.useRabbitMQ);
    factory.setMessageConverter(jsonMessageConverter());
    return factory;
}