代码之家  ›  专栏  ›  技术社区  ›  avalon

由于缺少rabbit工厂,Spring boot集成测试失败

  •  1
  • avalon  · 技术社区  · 7 年前

    我有一个使用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("");
        }
    }
    

    我得到异常兔子连接工厂没有找到!但我不应该初始化它,因为SpringBoot应该这样做。报告说,没有找到连接工厂bean的候选项,预计至少有一个。如何在使用rabbitmq的应用程序中编写测试?

    1 回复  |  直到 7 年前
        1
  •  -1
  •   Lho Ben    7 年前

    您需要用enablebrabbit注释您的测试类:

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

    模拟工厂,连接和通道。

    @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("");
        }
    }