如何使用jms.messagedrivenchanneladapter测试集成流

gupuwyp2  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(400)

我尝试测试integrationflow,它接收jms消息,然后执行一些逻辑,将其发送到另一个jms队列,然后将其保存到文件中。
我有这样的想法:

public class ProcessingFlow {
    @Bean
    public IntegrationFlow jmsProcessingFlow(
            ActiveMQConnectionFactory activeMQConnectionFactory,
            ValidationService validationService,
            ConverterService converterService,
            JmsMessageSender jmsMessageSender,
            @Value("${folder.location}") String folderLocation) {
        return IntegrationFlows.from(Jms.messageDrivenChannelAdapter(activeMQConnectionFactory).destination("JMSQueueFrom").id("jmsProcessing"))
                .<String>handle((incomeFilePayload, messageHeaders) -> {
                    validationService.validate(incomeFilePayload);
                    String message = converterService.convert(incomeFilePayload);
                    jmsMessageSender.sendMessage("JMSQueueTo", message);
                    return incomeFilePayload;
                })
                .handle(Files.outboundAdapter(new File(folderLocation))
                        .fileNameGenerator("file.xml"))
                .get();
    }
}

我写了这样的集成测试:

@RunWith(SpringRunner.class)
@SpringIntegrationTest
@SpringBootTest(classes = {ProcessingFlowIntegrationTest.TestConfig.class})
@ActiveProfiles("test")
@DirtiesContext
public class ProcessingFlowIntegrationTest {

    @MockBean
    private ValidationService validationService;

    @SpyBean
    private ConverterService converterService;

    @Autowired
    private MockIntegrationContext mockIntegrationContext;

    @After
    public void tearDown() {
        this.mockIntegrationContext.resetBeans();
    }

    @Test
    public void shouldTestProcessingFlow() throws Exception {
        TextMessage message = mock(TextMessage.class);
        when(message.getText()).thenReturn(XmlContentEnum.CONTENT.getContent());
    this.mockIntegrationContext.substituteMessageSourceFor("jmsProcessing", MockIntegration.mockMessageSource(message));

        verify(validationService).validate(eq(message.getText()));
        verify(converterService).convert(eq(message.getText()));
    }

    @TestConfiguration
    @Import(value = {
            ProcessingFlow.class,
            JmsConfiguration.class,
            TestConfig.class
    })
    static class TestConfig {

    }

}

但在运行之后,我收到了这样的异常:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'jmsProcessing' is expected to be of type 'org.springframework.integration.endpoint.SourcePollingChannelAdapter' but was actually of type 'org.springframework.integration.jms.JmsMessageDrivenEndpoint'

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:399)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
at org.springframework.integration.test.context.MockIntegrationContext.substituteMessageSourceFor(MockIntegrationContext.java:215)
at org.springframework.integration.test.context.MockIntegrationContext.substituteMessageSourceFor(MockIntegrationContext.java:157)
at org.springframework.integration.test.context.MockIntegrationContext.substituteMessageSourceFor(MockIntegrationContext.java:142)
at com.postpunk.flows.integration.ProcessingFlowIntegrationTest.shouldTestProcessingFlow(ProcessingFlowIntegrationTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我从异常中了解到,bean的类型为“org.springframework.integration.jms.jmsmessagedrivenendpoint”,但mockintegrationcontext应为“org.springframework.integration.endpoint.sourcepollingchanneladapter”。有人知道如何正确测试jmsmessagedrivenendpoint以及我做错了什么吗?

w6mmgewl

w6mmgewl1#

noAutoStartup 上的消息驱动终结点 @SpringIntegrationTest .
自动连线 IntegrationFlow .
那么 flow.getInputChannel().send(...);

相关问题