Spring集成-两端使用PublishSubscribeChannel通道的路由流的单元测试

时间:2019-01-24 09:14:21

标签: spring-integration

我们需要通过项目中的单元测试来覆盖routeCoreServiceRequestsAndEvents。以下是我们配置的摘录。该流的输出通道是其他流的输入通道,所以这就是为什么它们都是PublishSubscribeChannel通道的原因。

我们正在使用spring-integration 5.0在我们的项目中路由消息。通常,根据传入消息中的属性,我们希望将消息路由到其他渠道

我想在单元测试中检查此路由是否正确。

    @Configuration
    @IntegrationComponentScan
    @EnableIntegration
    public class SampleConfig {
        @Bean
        public IntegrationFlow routeCoreServiceRequestsAndEvents(@Qualifier("executor") final TaskExecutor executor) {
            return IntegrationFlows.from(() -> coreServiceResponseRoutingChannel(), e -> e.id("routeCoreServiceRequestsAndEventsEndPoint"))
                    .<ConnectorCoreServiceMessage, ConnectorServiceRoutingType>
                            route(this::resolveCTIEventsRouting, m -> m
                            .channelMapping(ConnectorServiceRoutingType.CONFIGURATION_EVENTS, "configurationEventMsgOutboundChannel")
                            .channelMapping(ConnectorServiceRoutingType.AGENT_EVENTS, "agentEventsMsgOutboundChannel")
                            .channelMapping(ConnectorServiceRoutingType.CALL_EVENTS, "callEventsMsgOutboundChannel")
                            .channelMapping(ConnectorServiceRoutingType.DATA_RESPONSE, "dataResponseMsgOutboundChannel")

                    )
                    .get();
        }

        @Bean
        public MessageChannel coreServiceResponseRoutingChannel() {
            return new PublishSubscribeChannel();
        }

        @Bean
        public MessageChannel configurationEventMsgOutboundChannel() {
            return new PublishSubscribeChannel();
        }

        @Bean
        public MessageChannel callEventsMsgOutboundChannel() {
            return new PublishSubscribeChannel();
        }

        @Bean
        public MessageChannel dataResponseMsgOutboundChannel() {
            return new PublishSubscribeChannel();
        }

        @Bean
        public MessageChannel agentEventsMsgOutboundChannel() {
            return new PublishSubscribeChannel();
        }
    }

我必须尝试以创建新处理程序的方式编写测试,以检查是否收到消息并订阅了此频道。像这样

    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = {SampleConfig.class,})
    @SpringIntegrationTest(noAutoStartup = {"*"})
    SampleConfigTest{

        @Autowired
        ApplicationContext applicationContext;

        @Autowired
        private MockIntegrationContext mockIntegrationContext;

        @Autowired
        @Qualifier("coreServiceResponseRoutingChannel")
        private PublishSubscribeChannel coreServiceResponseRoutingChannel;

        @Autowired
        @Qualifier("amqpConfigurationEventsMsgOutboundEndPoint")
        private AbstractEndpoint amqpConfigurationEventsMsgOutboundEndPoint;

        @Autowired
        @Qualifier("routeCoreServiceRequestsAndEventsEndPoint")
        private AbstractEndpoint routeCoreServiceRequestsAndEventsEndPoint;

        @Autowired
        @Qualifier("dataResponseMsgOutboundChannel")
        private PublishSubscribeChannel dataResponseMsgOutboundChannel;

        @Test
        public void testMyFlow(){
            this.mockIntegrationContext.resetBeans();
            ArgumentCaptor<Message<?>> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
                    MessageHandler handler =
                            MockIntegration.mockMessageHandler(messageArgumentCaptor)
                                    .handleNext(m -> {
                                        System.out.println("*******************************Handler triggered ************************");
                                    });

            routeCoreServiceRequestsAndEventsEndPoint.start();

            onfigurationEventMsgOutboundChannel.subscribe(handler);
            agentEventsMsgOutboundChannel.subscribe(handerX);
            callEventsMsgOutboundChannel.subscribe(handlerY);
            dataResponseMsgOutboundChannel.subscribe(handlerZ);

            mockIntegrationContext.substituteMessageSourceFor("routeCoreServiceRequestsAndEventsEndPoint",
                        MockIntegration.mockMessageSource(connectorCoreServiceMessage));

            //this.coreServiceResponseRoutingChannel.send(message); (NOT Working too)

            aserts 
            .....
        }

    }

但是我遇到以下错误,没有消息发送到流

DEBUG org.springframework.integration.dispatcher.BroadcastingDispatcher - No subscribers, default behavior is ignore

我进行了调查,发现此消息的原因是流定义中缺少通道。当我使用.channel对流进行测试时,已发送消息并注册了处理程序。

有什么想法吗?

预先感谢

0 个答案:

没有答案