如何在maven测试项目中使用rabbitmq?

mrwjdhj3  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(275)

有一个测试框架maven项目使用以下与spring相关的库来运行cucumber测试,但它不是spring引导项目:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>${cucumber.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${springframework.version}</version>
</dependency>

还有几个spring boot应用程序微服务来处理连接的硬件(移动电话和蓝牙设备),它们使用cloud stream rabbitmq binder相互通信:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream</artifactId>
</dependency>

目前,我们将测试自动化maven项目中的rest请求发送到这些spring应用程序,但我们认为发送消息会更好。
我试过在测试项目中使用rabbit,就像在springboot项目中使用一样,但是看起来不可能。
第一个问题是:是否可以动态地创建通道并连接到其他微服务所连接的同一个rabbit服务?
我还尝试将maven项目转换为spring boot,添加了上面提到的spring云依赖项,并使用mvntest命令运行cucumber测试。我还在application.properties中添加了以下属性:


# Messaging system

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

# Messaging channels

spring.cloud.stream.bindings.eurekaOutChannel.destination=eurekaChannel
spring.cloud.stream.bindings.eurekaInChannel.destination=eurekaChannel

但据我在日志中看到的,rabbit活页夹和频道并没有自动创建,只有默认的errorchannel。

2021-03-31 14:04:44.199  INFO 31740 --- [           main] r.utilities.hooks.common.CucumberHooks   : No active profile set, falling back to default profiles: default
2021-03-31 14:04:44.915  INFO 31740 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2021-03-31 14:04:44.926  INFO 31740 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2021-03-31 14:04:44.934  INFO 31740 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2021-03-31 14:04:44.981  INFO 31740 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationChannelResolver' of type [org.springframework.integration.support.channel.BeanFactoryChannelResolver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-03-31 14:04:44.989  INFO 31740 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-03-31 14:04:45.522  INFO 31740 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2021-03-31 14:04:45.722  INFO 31740 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2021-03-31 14:04:45.724  INFO 31740 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'FarmTest.errorChannel' has 1 subscriber(s).
2021-03-31 14:04:45.726  INFO 31740 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2021-03-31 14:04:45.737  INFO 31740 --- [           main] r.utilities.hooks.common.CucumberHooks   : Started CucumberHooks in 2.141 seconds (JVM running for 28.5)

同时,当我运行我们的一个spring boot微服务时,rabbit binder和频道会自动创建:

2021-03-29 10:36:44.363  INFO 21608 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'FarmCoreService.androidInChannel' has 1 subscriber(s).
2021-03-29 10:36:44.364  INFO 21608 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'FarmCoreService.eurekaInChannel' has 1 subscriber(s).
2021-03-29 10:36:44.364  INFO 21608 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'FarmCoreService.hiInChannel' has 1 subscriber(s).
2021-03-29 10:36:44.369  INFO 21608 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2021-03-29 10:36:44.370  INFO 21608 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'FarmCoreService.errorChannel' has 1 subscriber(s).
2021-03-29 10:36:44.370  INFO 21608 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2021-03-29 10:36:44.373  INFO 21608 --- [           main] o.s.c.s.binder.DefaultBinderFactory      : Creating binder: rabbit
2021-03-29 10:36:47.188  INFO 21608 --- [           main] o.s.c.s.binder.DefaultBinderFactory      : Caching the binder: rabbit
2021-03-29 10:36:47.188  INFO 21608 --- [           main] o.s.c.s.binder.DefaultBinderFactory      : Retrieving cached binder: rabbit
2021-03-29 10:36:47.248  INFO 21608 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [127.0.0.1:5672]
2021-03-29 10:36:47.310  INFO 21608 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#1de13f34:0/SimpleConnection@2404abe2 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 63887]
2021-03-29 10:36:47.348  INFO 21608 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'FarmCoreService.eurekaOutChannel' has 1 subscriber(s).
2021-03-29 10:36:47.350  INFO 21608 --- [           main] o.s.c.s.binder.DefaultBinderFactory      : Retrieving cached binder: rabbit
2021-03-29 10:36:47.517  INFO 21608 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'FarmCoreService.hiOutChannel' has 1 subscriber(s).
2021-03-29 10:36:47.517  INFO 21608 --- [           main] o.s.c.s.binder.DefaultBinderFactory      : Retrieving cached binder: rabbit
2021-03-29 10:36:47.596  INFO 21608 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'FarmCoreService.androidOutChannel' has 1 subscriber(s).
2021-03-29 10:36:47.597  INFO 21608 --- [           main] o.s.c.s.binder.DefaultBinderFactory      : Retrieving cached binder: rabbit

那么第二个问题:maven测试项目配置有什么问题,它不能自动创建rabbit绑定器和通道?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题