spring-integration - Spring集成流测试
问题描述
我正在尝试测试
@Bean
public IntegrationFlow integrationFlow(JobLaunchingGateway jobLaunchingGateway) {
return IntegrationFlows.from(inventoryImportInboundChannelAdapter,
p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency()))).
handle(fileMessageToPath()).
handle(fileMessageToJobRequest()).
handle(jobLaunchingGateway).
log(LoggingHandler.Level.INFO, "headers.id + ': ' + payload").
get();
}
库存导入通道适配器是 s3 适配器,我不想连接到 S3 进行组件测试。我尝试使用 MockIntegrationContext 但它不起作用。请指教
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ImportInventoryJobIntegrationFlow.class})
@SpringIntegrationTest
public class ImportInventoryJobIntegrationFlowTest {
@MockBean
private MessageSource<?> inventoryImportInboundChannelAdapter;
@MockBean
private Job inventoryImportJob;
@MockBean
private JobRepository jobrepository;
@MockBean
private InventoryImportJobProperties inventoryImportJobProperties;
@Autowired
private MockIntegrationContext mockIntegrationContext;
@Test
public void testChannelAdapter(){
File importFile = Mockito.mock(File.class);
BDDMockito.given(importFile.getParent()).willReturn("test.import");
System.out.println(mockIntegrationContext);
this.mockIntegrationContext.substituteMessageSourceFor("inventoryImportInboundChannelAdapter",
MockIntegration.mockMessageSource(importFile));
}
}
错误获取是:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“inventoryImportInboundChannelAdapter”的bean可用
解决方案
请参阅mockIntegrationContext.substituteMessageSourceFor()
JavaDocs:
/**
* Replace the real {@link MessageSource} in the {@link SourcePollingChannelAdapter} bean
* with provided {@link MessageSource} instance.
* Can be a mock object.
* @param pollingAdapterId the endpoint bean name
* @param mockMessageSource the {@link MessageSource} to replace in the endpoint bean
* @see org.springframework.integration.test.mock.MockIntegration#mockMessageSource
*/
public void substituteMessageSourceFor(String pollingAdapterId, MessageSource<?> mockMessageSource) {
那里有关键词SourcePollingChannelAdapter
。这个 bean 是你的结果
IntegrationFlows.from(inventoryImportInboundChannelAdapter,
p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency())))
不幸的是,您没有在此处指定 that inventoryImportInboundChannelAdapter
,因此会生成其目标名称。
考虑在该端点的定义.id("inventoryImportInboundChannelAdapter")
之前或之后添加。poller()
更新
我们有一个这样的测试配置:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows
.from(() -> new GenericMessage<>("myData"),
e -> e.id("mySourceEndpoint"))
.<String, String>transform(String::toUpperCase)
.channel(results())
.get();
}
注意e.id("mySourceEndpoint")
. 然后在测试中我们这样做:
this.mockIntegrationContext.substituteMessageSourceFor("mySourceEndpoint",
MockIntegration.mockMessageSource("foo", "bar", "baz"));
推荐阅读
- gradle - gradle 的新插件 dsl 语法
- google-chrome - 如何使用代码或 API 从 chrome 开发人员工具中自动获取 fps 仪表值?
- qt - fileDialogDCM.fileUrls.count 给出“未定义”(QML)
- math - What's the time complexity name for O(n*m) (ex: linear, constant)?
- html - 可滚动容器上的引导下拉定位
- regex - JMeter正则表达式提取器不在单引号内提取
- typescript - 如何为第三个库添加打字文件?
- mysql - 如何在自动增量列中设置特定值?
- ios - 离屏渲染金属
- c# - ASP.NET MVC 5 正则表达式属性无法验证密码