gpt4 book ai didi

java - 单元测试 Spring 集成流 DSL

转载 作者:行者123 更新时间:2023-12-04 16:08:15 25 4
gpt4 key购买 nike

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

集成流

@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());
}
}

提前致谢

最佳答案

这正是我们正在做的场景now使用 MockMessageHandler 实现。

看起来你在模拟中采取了正确的方式来防止 fileNotFoundFlow 中的进一步操作,但错过了一些简单的技巧:

您必须在那个 fileNotFoundChannelstop() 真正的 .handle((payload, headers) ) 端点。这样它就会取消订阅 channel 并且不再消费消息。为此,我建议这样做:

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());
}

请注意,在向 channel 发送消息之前,我是如何移动 mock 和订阅的。

借助新的 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();
}

}

}

关于java - 单元测试 Spring 集成流 DSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com