【发布时间】: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