如何编写测试我的具有输入通道和输出通道的服务激活器
How to write test my service activator which has input channel and output channel
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>
============================================= ================================================ =================================================
这是我的测试class,我想测试我的服务激活器以确保消息到达这些频道
@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();
}
}
在您的测试用例中,为每个通道添加一个ChannelInterceptor
,您可以验证截获的消息。
编辑
像这样:
@Test
void test(@Autowired AbstractMessageChannel inChannel,
@Autowired AbstractMessageChannel outChannel) throws InterruptedException {
AtomicReference<Message<?>> inMessage = new AtomicReference<>();
inChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
inMessage.set(message);
return message;
}
});
AtomicReference<Message<?>> outMessage = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
outChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
outMessage.set(message);
latch.countDown();
return message;
}
});
// send test message to JMS (or directly to inChannel for a unit test)
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
// verify inMessage.get() is as expected
// verify outMessage.get() is as expected
}
这假设 JUnit5;如果您使用的是 JUnit4,请改为在测试中将通道自动连接为字段。
EDIT2
这是完整的应用程序;对我来说很好。
@SpringBootApplication
public class So71942436Application {
public static void main(String[] args) {
SpringApplication.run(So71942436Application.class, args);
}
@ServiceActivator(inputChannel = "inChannel", outputChannel = "outChannel")
public Message<?> handle(Message<?> msg) {
return msg;
}
@ServiceActivator(inputChannel = "outChannel")
public void dump(Message<?> msg) {
System.out.println(msg);
}
}
@SpringBootTest
class So71942436ApplicationTests {
@Test
void test(@Autowired AbstractMessageChannel inChannel,
@Autowired AbstractMessageChannel outChannel) throws InterruptedException {
AtomicReference<Message<?>> inMessage = new AtomicReference<>();
inChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
inMessage.set(message);
return message;
}
});
AtomicReference<Message<?>> outMessage = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
outChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
outMessage.set(message);
latch.countDown();
return message;
}
});
inChannel.send(new GenericMessage<>("foo"));
// send test message to JMS
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
// verify inMessage.get() is as expected
// verify outMessage.get() is as expected
}
}
测试是绿色的。
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>
============================================= ================================================ =================================================
这是我的测试class,我想测试我的服务激活器以确保消息到达这些频道
@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();
}
}
在您的测试用例中,为每个通道添加一个ChannelInterceptor
,您可以验证截获的消息。
编辑
像这样:
@Test
void test(@Autowired AbstractMessageChannel inChannel,
@Autowired AbstractMessageChannel outChannel) throws InterruptedException {
AtomicReference<Message<?>> inMessage = new AtomicReference<>();
inChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
inMessage.set(message);
return message;
}
});
AtomicReference<Message<?>> outMessage = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
outChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
outMessage.set(message);
latch.countDown();
return message;
}
});
// send test message to JMS (or directly to inChannel for a unit test)
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
// verify inMessage.get() is as expected
// verify outMessage.get() is as expected
}
这假设 JUnit5;如果您使用的是 JUnit4,请改为在测试中将通道自动连接为字段。
EDIT2
这是完整的应用程序;对我来说很好。
@SpringBootApplication
public class So71942436Application {
public static void main(String[] args) {
SpringApplication.run(So71942436Application.class, args);
}
@ServiceActivator(inputChannel = "inChannel", outputChannel = "outChannel")
public Message<?> handle(Message<?> msg) {
return msg;
}
@ServiceActivator(inputChannel = "outChannel")
public void dump(Message<?> msg) {
System.out.println(msg);
}
}
@SpringBootTest
class So71942436ApplicationTests {
@Test
void test(@Autowired AbstractMessageChannel inChannel,
@Autowired AbstractMessageChannel outChannel) throws InterruptedException {
AtomicReference<Message<?>> inMessage = new AtomicReference<>();
inChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
inMessage.set(message);
return message;
}
});
AtomicReference<Message<?>> outMessage = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
outChannel.addInterceptor(new ChannelInterceptor() {
@Override
@Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
outMessage.set(message);
latch.countDown();
return message;
}
});
inChannel.send(new GenericMessage<>("foo"));
// send test message to JMS
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
// verify inMessage.get() is as expected
// verify outMessage.get() is as expected
}
}
测试是绿色的。