英文:
Rabbit trying to connect on test spring boot
问题
我想在@Test方法中禁用Rabbit。我尝试使用doNothing.when(rabbit.class)方法,但它不起作用。
你能帮我找到阻止连接到Rabbit的方法吗?
谢谢。
英文:
I want to disable the Rabbit on @Test method. I tried using the doNothing.when(rabbit.class) method, but it's not working.
Can you help me find a way to prevent the connection to Rabbit?
Thanks
2023-02-06 19:39:58.519 INFO 75 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: localhost:5672
2023-02-06 19:39:58.522 ERROR 75 --- [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:602) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:725) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:252) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2180) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2153) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2133) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:463) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:447) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1930) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1911) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1377) ~[spring-rabbit-2.4.7.jar:2.4.7]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1223) ~[spring-rabbit-2.4.7.jar:2.4.7]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.ConnectException: Connection refused
答案1
得分: 1
这取决于你要做什么,如果你正在运行单元测试,那不应该是个问题,因为单元测试不需要加载应用程序上下文。另一方面,如果你正在运行集成测试@SpringBootTest
,那就需要加载应用程序上下文并注入你的Beans。你有两个解决方案:
- 要么在需要 RabbitMq 的服务上加上
@MockBean
注解。 - 使用测试容器(test containers)来启动一个 RabbitMq 测试环境。
提供更多关于你正在运行的测试类型以及你如何编写测试的细节会更有帮助。
英文:
This depends on what you are trying to do if you running unit tests that should not be a problem since unit tests do not require loading the application context. On the other hand, if you running integration tests @SpringBootTest
that requires you to load your application context and inject your Beans. You have 2 solutions:
- Either annotate your services that require RabbitMq with
@MockBean
. - Use test containers to spin up a rabbitMq testing environment.
It would be more helpful to provide more details about the kind of test you are running and how you have written your test.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论