使用Spring和Mockito测试RabbitMQ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Spring和Mockito测试RabbitMQ相关的知识,希望对你有一定的参考价值。
尝试使用RabbitTemplate进行Spring JUnit运行测试,并使用Mockito存根服务类注入侦听器。试图验证与模拟的交互。通过我见过的例子,我认为这是可能的。 RabbitMQ正在运作。登录仪表板时,我可以在那里看到消息。能够使用独立控制台应用程序使用消息。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/servlet-context.xml", "classpath:/spring/root-context.xml", "classpath:rabbitlistener-context-test.xml"})
public class ReceiveOrderQueueListenerTest {
@Mock
private ReceiveOrderRepository mockRepos;
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
SimpleMessageListenerContainer listenerContainer;
@InjectMocks
@Autowired
ReceiveOrderQueueListener receiveOrderQueueListener;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testAbleToReceiveMessage() {
RequestForService rfs = new RequestForService();
rfs.setClaimNumber("a claim");
rabbitTemplate.convertAndSend("some.queue", rfs);
verify(mockRepos).save(new OrderRequest());
}
}
然后兔子听众配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<rabbit:connection-factory id="rabbitConnectionFactory" host="XXXXXXXXX.com" username="test" password="test" />
<!-- <rabbit:connection-factory id="rabbitConnectionFactory" host="localhost" username="guest" password="guest" /> -->
<rabbit:admin connection-factory="rabbitConnectionFactory" auto-startup="true" />
<rabbit:template id="tutorialTemplate" connection-factory="rabbitConnectionFactory" exchange="TUTORIAL-EXCHANGE"/>
<rabbit:queue name="some.queue" id="some.queue"></rabbit:queue>
<bean id="receiveOrderListener" class="XXXXXXXXXX.connect.message.ReceiveOrderQueueListenerImpl"></bean>
<rabbit:topic-exchange id="myExchange" name="TUTORIAL-EXCHANGE">
<rabbit:bindings>
<rabbit:binding queue="some.queue" pattern="some.queue"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container connection-factory="rabbitConnectionFactory">
<!-- <rabbit:listener queues="some.queue" ref="receiveOrderListener" method="handleMessage"/> -->
<rabbit:listener queues="some.queue" ref="receiveOrderListener" />
</rabbit:listener-container>
</beans>
尝试在MessgeListenerAdaptor中进行注入,并考虑所需的测试,以便正确连接侦听器。注入适配器后,我能够验证委托是否已经注入了存根。测试失败,没有与mock的零交互。我可以登录rabbitmq,消息就在那里。在此测试运行期间,注入的侦听器对象不会消耗队列中的消息。
听众说,差点忘了。尝试默认签名并尝试指定方法。
public class ReceiveOrderQueueListenerImpl implements ReceiveOrderQueueListener {
@Autowired
ReceiveOrderRepository receiveOrderRepository;
@Override
public void handleMessage(RequestForService rfs) {
System.out.println("receive a message");
receiveOrderRepository.save(new OrderRequest());
}
public void onMessage(Message message) {
receiveOrderRepository.save(new OrderRequest());
}
}
任何建议都会有所帮助,我提前感谢您的帮助。
我在测试中看不到任何同步。消息传递本质上是异步的(意味着巡回测试可以在消息到达之前完成)。
尝试使用Latch概念,该概念会阻止消息到达或超时到期。
首先,您需要一个队列的测试侦听器bean:
@Bean
@lombok.RequiredArgsContructor
@lombok.Setter
public class TestListener {
private final ReceiveOrderQueueListener realListener;
private CountDownLatch latch;
public void onMessage(Message message) {
realListener.onMessage(message);
latch.countDown();
}
}
确保将其配置为在测试上下文中用于代替原始侦听器。
然后在你的测试中你可以设置闩锁:
public class ReceiveOrderQueueListenerTest {
@Autowired
private TestListener testListener;
@Test
public void testAbleToReceiveMessage() {
RequestForService rfs = new RequestForService();
rfs.setClaimNumber("a claim");
// Set latch for 1 message.
CountDownLatch latch = new CountDownLatch(1);
testListener.setLatch(latch);
rabbitTemplate.convertAndSend("some.queue", rfs);
// Wait max 5 seconds, then do assertions.
latch.await(5, TimeUnit.SECONDS);
verify(mockRepos).save(new OrderRequest());
}
}
请注意,有更好的方法如何使用锁存器(例如通道拦截器或LatchCountDownAndCallRealMethodAnswer
与Mockito),但这是基本的想法。有关更多信息,请参阅Spring AMQP testing reference。
以上是关于使用Spring和Mockito测试RabbitMQ的主要内容,如果未能解决你的问题,请参考以下文章
使用 mockito [Spring-Boot] [重复] 测试 void 函数 try/catch 块