【问题标题】:Spring boot integration test fails because of missing rabbit factory由于缺少兔子工厂,Spring Boot 集成测试失败
【发布时间】:2018-12-09 06:58:12
【问题描述】:

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

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

【问题讨论】:

  • 也许您需要将@EnableRabbit 添加到您的配置中并在您的测试中使用该配置

标签: java testing rabbitmq integration-testing amqp


【解决方案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("");
    }
}

【讨论】:

  • 谢谢,但此配置给出错误:java.lang.IllegalStateException:配置错误:为测试类 [com.eye4fraud.domaintools.DomainToolsWorkerIT] 找到多个 @BootstrapWith 声明:[@org.springframework.test .context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper )]
  • 你也可以分享进口的例子吗?我找不到带有 createchannel 方法的 Channel 类
  • 我也得到 java.lang.IllegalStateException: 测试类不能包含 @Bean 方法
  • 同样在将 bean 移动到配置 bean 之后: 原因:org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Connection$MockitoMock$1579221884 不能由 createConnection() 返回 createConnection() 应该返回 Connection
猜你喜欢
  • 2016-01-21
  • 1970-01-01
  • 2019-12-26
  • 2017-07-02
  • 2011-12-29
  • 1970-01-01
  • 2020-03-18
  • 2021-03-20
  • 1970-01-01
相关资源
最近更新 更多