Spring 集成流测试
Spring integrationflow test
我正在尝试测试
@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
。这个豆子是你
IntegrationFlows.from(inventoryImportInboundChannelAdapter,
p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency())))
很遗憾,您没有在此处指定 inventoryImportInboundChannelAdapter
,因此生成了它的目标名称。
考虑在该端点的 poller()
定义之前或之后添加 .id("inventoryImportInboundChannelAdapter")
。
更新
我们有这样的测试配置:
@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"));
我正在尝试测试
@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
。这个豆子是你
IntegrationFlows.from(inventoryImportInboundChannelAdapter,
p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency())))
很遗憾,您没有在此处指定 inventoryImportInboundChannelAdapter
,因此生成了它的目标名称。
考虑在该端点的 poller()
定义之前或之后添加 .id("inventoryImportInboundChannelAdapter")
。
更新
我们有这样的测试配置:
@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"));