兔子试图连接测试Spring Boot。

huangapple go评论65阅读模式
英文:

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。你有两个解决方案:

  1. 要么在需要 RabbitMq 的服务上加上 @MockBean 注解。
  2. 使用测试容器(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:

  1. Either annotate your services that require RabbitMq with @MockBean.
  2. 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.

huangapple
  • 本文由 发表于 2023年2月7日 03:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定