首页 > 解决方案 > 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可用

标签: spring-integrationspring-integration-dsl

解决方案


请参阅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"));

推荐阅读