【问题标题】:Spring integrationflow testSpring集成流测试
【发布时间】:2024-05-22 23:15:01
【问题描述】:

我正在尝试测试

 @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-integration spring-integration-dsl


    【解决方案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,所以它的目标名称是生成的。

    考虑在该端点的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"));
    

    【讨论】:

    • 已添加,p -> p.poller(Pollers.fixedRate(inventoryImportJobProperties.getPollingFrequency())).id("inventoryImportPoller"))。仍然显示 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inventoryImportPoller' available
    • 在我的回答中查看更新。也许问题出在其他地方。请为该NoSuchBeanDefinitionException 显示更多堆栈跟踪。
    • 没关系,我使用了一个没有名字的 mockbean 导致了这个问题。感谢您的时间和支持
    • 是的,请。另请注意,@EnableIntegration 位于主配置类中,而不是我为此特定测试加载的配置中
    • 嗯?我在你的测试中看到了@SpringBootTest,所以决定你对这件事的配置很好。很高兴知道问题已经解决了!由于您是请求者,因此只有您可以接受答案。
    最近更新 更多