首页 文章

由于缺少兔子工厂,Spring启动集成测试失败

提问于
浏览
1

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

我发现没有找到兔子连接工厂的例外!但我不应该启动它,因为 Spring 季启动应该这样做 . 它说找不到ConnectionFactory bean的候选者,预计至少有一个 . 我如何在使用rabbitmq的应用程序中编写测试?

1 回答

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

相关问题