3

我正在尝试对一个简单的流程进行单元测试,它正在检查文件是否存在,然后执行一些额外的任务。

集成流

@Bean
public IntegrationFlow initiateAlarmAck() {
    return IntegrationFlows.from("processAckAlarmInputChannel")
            .handle((payload, headers) ->  {
                LOG.info("Received initiate ack alarm request at " + payload);
                File watermarkFile = getWatermarkFile();
                if(watermarkFile.isFile()){
                    LOG.info("Watermark File exists");
                    return true;
                }else{
                    LOG.info("File does not exists");
                    return false;
                }
            })
            .<Boolean, String>route(p -> fileRouterFlow(p))
            .get();
}
File getWatermarkFile(){
    return new File(eventWatermarkFile);
}

@Router
public String fileRouterFlow(boolean fileExits){
    if(fileExits)
        return "fileFoundChannel";
    else
        return "fileNotFoundChannel";
}

还有另一个集成流程从中挑选消息fileNotFoundChannel并进行额外处理。我不想对这部分进行单元测试。如何停止我的测试而不做进一步的测试并在发布消息后停止fileNotFoundChannel

@Bean
public IntegrationFlow fileNotFoundFlow() {
    return IntegrationFlows.from("fileNotFoundChannel")
            .handle((payload, headers) ->  {
                LOG.info("File Not Found");
                return payload;
            })
            .handle(this::getLatestAlarmEvent)
            .handle(this::setWaterMarkEventInFile)
            .channel("fileFoundChannel")
            .get();
}

单元测试类

@RunWith(SpringRunner.class)
@Import(AcknowledgeAlarmEventFlow.class)
@ContextConfiguration(classes = {AlarmAPIApplication.class})
@PropertySource("classpath:application.properties ")
public class AcknowledgeAlarmEventFlowTest {


    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    @Qualifier("processAckAlarmInputChannel")
    DirectChannel processAckAlarmInputChannel;

    @Autowired
    @Qualifier("fileNotFoundChannel")
    DirectChannel fileNotFoundChannel;

    @Autowired
    @Qualifier("fileFoundChannel")
    DirectChannel fileFoundChannel;

    @Mock
    File mockFile;

    @Test
    public void initiateAlarmAck_noFileFound_verifyMessageOnfileNotFoundChannel(){


        AcknowledgeAlarmEventFlow.ProcessAcknowledgeAlarmGateway gateway = applicationContext.getBean(AcknowledgeAlarmEventFlow.ProcessAcknowledgeAlarmGateway.class);
        gateway.initiateAcknowledgeAlarm();

        processAckAlarmInputChannel.send(MessageBuilder.withPayload(new Date()).build());
        MessageHandler mockMessageHandler = mock(MessageHandler.class);

        fileNotFoundChannel.subscribe(mockMessageHandler);
        verify(mockMessageHandler).handleMessage(any());
    }
}

提前致谢

4

1 回答 1

8

这正是我们现在正在执行的MockMessageHandler方案。

看起来你在嘲笑中采取了正确的方式来防止进一步的行动fileNotFoundFlow,但错过了一些简单的技巧:

你必须到stop()那个真正的.handle((payload, headers) )端点fileNotFoundChannel。这样它就会取消订阅频道并且不再消费消息。为此,我建议这样做:

return IntegrationFlows.from("fileNotFoundChannel")
.handle((payload, headers) ->  {
  LOG.info("File Not Found");
  return payload;
}, e -> e.id("fileNotFoundEndpoint"))

在测试课上

@Autowired
@Qualifier("fileNotFoundEndpoint")
AbstractEndpoint fileNotFoundEndpoint;
 ...

@Test
public void initiateAlarmAck_noFileFound_verifyMessageOnfileNotFoundChannel(){
  this.fileNotFoundEndpoint.stop();

  MessageHandler mockMessageHandler = mock(MessageHandler.class);

  fileNotFoundChannel.subscribe(mockMessageHandler);


  AcknowledgeAlarmEventFlow.ProcessAcknowledgeAlarmGateway gateway = applicationContext.getBean(AcknowledgeAlarmEventFlow.ProcessAcknowledgeAlarmGateway.class);
  gateway.initiateAcknowledgeAlarm();

  processAckAlarmInputChannel.send(MessageBuilder.withPayload(new Date()).build());
  verify(mockMessageHandler).handleMessage(any());
}

请注意,在向频道发送消息之前,我是如何移动嘲笑和订阅的。

借助新MockIntegrationContext功能,框架将为您处理这些内容。但是是的......与任何单元测试一样,必须在交互之前准备好模拟。

更新

工作样本:

@RunWith(SpringRunner.class)
@ContextConfiguration
public class MockMessageHandlerTests {

@Autowired
private SubscribableChannel fileNotFoundChannel;

@Autowired
private AbstractEndpoint fileNotFoundEndpoint;

@Test
@SuppressWarnings("unchecked")
public void testMockMessageHandler() {
    this.fileNotFoundEndpoint.stop();

    MessageHandler mockMessageHandler = mock(MessageHandler.class);

    this.fileNotFoundChannel.subscribe(mockMessageHandler);

    GenericMessage<String> message = new GenericMessage<>("test");
    this.fileNotFoundChannel.send(message);

    ArgumentCaptor<Message<?>> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);

    verify(mockMessageHandler).handleMessage(messageArgumentCaptor.capture());

    assertSame(message, messageArgumentCaptor.getValue());
}

@Configuration
@EnableIntegration
public static class Config {

    @Bean
    public IntegrationFlow fileNotFoundFlow() {
        return IntegrationFlows.from("fileNotFoundChannel")
        .<Object>handle((payload, headers) -> {
            System.out.println(payload);
            return payload;
        }, e -> e.id("fileNotFoundEndpoint"))
        .channel("fileFoundChannel")
        .get();
    }

}

}
于 2017-05-17T23:17:47.700 回答