如何编写具有输入通道和输出频道的服务激活器
I am trying to write a small test to test if the message arrived at the input channel is processed to the output channel as written in my listener code? Any comments on how can I start?
Update: I have added XML and test class could you please help me guide on how to write the test for this? Do i need to add the channel Interceptor in the test config class or in the same test class where I add the test method?
@Override
@ServiceActivator(inputChannel = "inChannel", outputChannel = "outChannel")
public Message<String> receiveEvent(Message<String> receivedEvent) {
return receivedEvent;
}
=============================================== ===============================================
**This is my jms-gateway.xml for the inbound gateway of JMS**
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
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.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<context:component-scan base-package="com.notification.application" />
<jms:inbound-gateway id="inboundGateway"
request-destination="inQueue"
error-channel="errorChannel"
request-channel="inChannel"
connection-factory="queueConnectionFactory"/>
</beans:beans>
=============================================== =============================================== ===
这是我的测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:jms-gateway.xml")
@DirtiesContext
@SpringBootTest
public class EventListenerTest {
@Autowired
@Qualifier("inChannel")
private DirectChannel messageChannel;
@MockBean
private EventListenerClass eventListener;
@Before
public void config() {
//how to do ?
messageChannel.addInterceptor();
}
@Test
void messageReceived_Test() {
Message<String> message = MessageBuilder.withPayload("Hello").build();
messageChannel.send(message);
Mockito.when(eventListener.receiveEvent(message)).thenReturn(message);
//how to do assertion on interceptor?
Assertions.assertThat(actualMessage).isNotNull();
}
}
I am trying to write a small test to test if the message arrived at the input channel is processed to the output channel as written in my listener code? Any comments on how can I start?
Update: I have added XML and test class could you please help me guide on how to write the test for this? Do i need to add the channel Interceptor in the test config class or in the same test class where I add the test method?
@Override
@ServiceActivator(inputChannel = "inChannel", outputChannel = "outChannel")
public Message<String> receiveEvent(Message<String> receivedEvent) {
return receivedEvent;
}
===========================================================
**This is my jms-gateway.xml for the inbound gateway of JMS**
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
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.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<context:component-scan base-package="com.notification.application" />
<jms:inbound-gateway id="inboundGateway"
request-destination="inQueue"
error-channel="errorChannel"
request-channel="inChannel"
connection-factory="queueConnectionFactory"/>
</beans:beans>
================================================================================================================================================
This is my Test class which I want to have test of my service activator to make sure message reach these channels
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:jms-gateway.xml")
@DirtiesContext
@SpringBootTest
public class EventListenerTest {
@Autowired
@Qualifier("inChannel")
private DirectChannel messageChannel;
@MockBean
private EventListenerClass eventListener;
@Before
public void config() {
//how to do ?
messageChannel.addInterceptor();
}
@Test
void messageReceived_Test() {
Message<String> message = MessageBuilder.withPayload("Hello").build();
messageChannel.send(message);
Mockito.when(eventListener.receiveEvent(message)).thenReturn(message);
//how to do assertion on interceptor?
Assertions.assertThat(actualMessage).isNotNull();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的测试案例中,将
ChanderInterceptor
添加到每个频道中,您可以验证截获的消息。编辑
类似的东西:
这是junit5;如果您使用的是JUNIT4,请自动将通道作为测试中的字段电线。
edit2
这是完整的应用程序;对我来说很好。
测试是绿色的。
In your test case, add a
ChannelInterceptor
to each of the channels and you can verify the intercepted messages.EDIT
Something like this:
This assumes JUnit5; if you are using JUnit4, auto wire the channels as fields in the test instead.
EDIT2
Here is the complete app; works fine for me.
Test is green.