gpt4 book ai didi

java - 使用 Spring 和 Mockito 测试 RabbitMQ

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:57:46 26 4
gpt4 key购买 nike

尝试让 Spring JUnit 运行器使用 RabbitTemplate 运行测试,并向监听器注入(inject) Mockito stub 服务类。尝试验证与 Mock 的交互。通过我见过的例子,我认为这是可能的。 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());
}

}

然后是 rabbit-listener 配置

<?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 中注入(inject),并认为测试也需要正确连接监听器。注入(inject)适配器后,我能够验证委托(delegate)是否被注入(inject) stub 。与模拟没有零交互时测试失败。我可以登录到 rabbitmq,消息就在那里。在此测试运行期间,注入(inject)的监听器对象未使用队列中的消息。

差点忘了,这位听众。尝试了默认签名并尝试了 designate 方法。

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());
}
}

请注意,有更好的方式来使用锁存器(例如 channel 拦截器或 Mockito 的 LatchCountDownAndCallRealMethodAnswer),但这是基本思想。参见 Spring AMQP testing reference了解更多信息。

关于java - 使用 Spring 和 Mockito 测试 RabbitMQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30697816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com