如何编写具有输入通道和输出频道的服务激活器

发布于 2025-01-23 05:15:52 字数 3511 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2025-01-30 05:15:52

在您的测试案例中,将ChanderInterceptor添加到每个频道中,您可以验证截获的消息。

编辑

类似的东西:

@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
    }

}

测试是绿色的。

In your test case, add a ChannelInterceptor to each of the channels and you can verify the intercepted messages.

EDIT

Something like this:

@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
}

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.

@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
    }

}

Test is green.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文