Spring Boot集成测试由于缺少Rabbit工厂而失败

时间:2018-12-08 22:46:31

标签: java testing rabbitmq integration-testing amqp

我有一个使用Rabbitmq并消耗消息的应用程序。我想编写一个集成测试来检查所有功能。我的配置如下:

@SpringBootApplication(scanBasePackages = {"com.mysite.domaintools", "com.mysite.core",
        "com.mysite.database.repository"})
@EntityScan("com.mysite.database.domain")
@EnableMongoRepositories(basePackages = {"com.mysite.database.repository.mongo"})
@EnableJpaRepositories("com.mysite.database.repository") @EnableRabbit
public class DomaintoolsApplication {

    private static final String topicExchangeName = "mysite";

    private static final String queueName = Queues.DOMAINTOOLS.getName();


    @Bean Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean TopicExchange exchange() {
        return new TopicExchange(topicExchangeName);
    }

    @Bean Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("domaintools.key.#");
    }

    @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean MessageListenerAdapter listenerAdapter(DomainToolsRabbitReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }


    public static void main(String[] args) {
        SpringApplication.run(DomaintoolsApplication.class, args);
    }
}

当我运行我的应用程序时一切都很好,但是当我尝试运行以下测试时:

@RunWith(SpringRunner.class)
@DataJpaTest
//@SpringBootTest
public class DomainToolsWorkerIT {

    @Autowired
    private DomainRepository domainRepository;

    @Test
    public void test(){
        System.out.println("");
    }
}

我得到未找到Rabbit连接工厂的异常!但是我不应该初始化它,因为Spring Boot应该这样做。它说没有找到ConnectionFactory bean的候选者,期望至少一个。如何在使用Rabbitmq的应用中编写测试?

1 个答案:

答案 0 :(得分:-1)

您需要使用EnableRabbit注释测试类:

并使用不同的模拟对象添加RabbitTemplate及其ConnectionFactory:

模拟工厂,连接和通道。

@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(classes = DomaintoolsApplication.class)
@EnableRabbit
public class DomainToolsWorkerIT {

    @Autowired
    private DomainRepository domainRepository;

         /**
         * Create test rabbit template to not load a real rabbitMQ instance.
         *
         * @return rabbit template.
         */
        @Bean
        public RabbitTemplate template() {
            return new RabbitTemplate(connectionFactory());
        }

        /**
         * Connection factory mock to create rabbit template.
         *
         * @return connection factory mock.
         */
        @Bean
        public ConnectionFactory connectionFactory() {
            ConnectionFactory factory = mock(ConnectionFactory.class);
            Connection connection = mock(Connection.class);
            Channel channel = mock(Channel.class);
            doReturn(connection).when(factory).createConnection();
            doReturn(channel).when(connection).createChannel(anyBoolean());
            doReturn(true).when(channel).isOpen();
            return factory;
        }

    @Test
    public void test(){
        System.out.println("");
    }
}