我正在尝试
@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
答案 0 :(得分:1)
请参考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())))
很遗憾,您没有在此处指定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"));