нам нужно покрыть routeCoreServiceRequestsAndEvents модульными тестами в нашем проекте. Ниже приведен фрагмент из нашей конфигурации. Выходные каналы этого потока являются входными каналами другого потока, поэтому все они являются каналами PublishSubscribeChannel.
Мы используем spring -gration 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, сообщение было отправлено, и обработчик был зарегистрирован.
Есть идеи?
Заранее спасибо