我正在尝试对一个简单的流程进行单元测试,它正在检查文件是否存在,然后执行一些额外的任务。
集成流
@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());
}
}
提前致谢