how to mock spring amqp/rabbit in spring boot test

I know this is an old topic, but I'd like to introduce a mocking library I'm developping : rabbitmq-mock.

The purpose of this mock is to mimic RabbitMQ behavior without IO (without starting a server, listening to some port, etc.) and with minor (~ none) startup time.

It is available in Maven Central:

<dependency>
    <groupId>com.github.fridujo</groupId>
    <artifactId>rabbitmq-mock</artifactId>
    <version>1.1.1</version>
    <scope>test</scope>
</dependency>

Basic use will be to override Spring configuration with a test one :

@Configuration
@Import(AmqpApplication.class)
class AmqpApplicationTestConfiguration {

    @Bean
    public ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory(MockConnectionFactoryFactory.build());
    }
}

For automatic mocking of Spring beans for tests, give a look at another project I'm working on: spring-automocker

Hope this can help !


Not sure if this is helpful but, I was having the same problem. So, I just used @MockBean on RabbitAdmin with a different profile, and did not get the same connection issues. Tests Passed.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@RunWith(SpringRunner.class)
@ActiveProfiles("my-test")
public class ServiceTests {

@Autowired
private DummyService unitUnderTest;

@MockBean
private RabbitAdmin rabbitAdmin;

// lots of tests which do not need Spring to Create a RabbitAdmin Bean
}