在SpringBoot应用程序中编写骆驼路线的单元测试-获取messageCount 0

时间:2018-10-31 01:43:15

标签: spring-boot apache-camel camel-test

我正在尝试为骆驼路线编写单元测试-用于导入和处理文件

from(fullImportFTP)
        .routeId(STUB_FILE_DOWNLOAD_ROUTE_ID)
        .onException(Exception.class)
        .handled(false)
        .log(LoggingLevel.ERROR, STUB_IMPORT_ERROR_CODE)
        .end()
        .log("Processing  Stub file:[${header.CamelFileName}]")
        .to(ROUTE_TO_MACE);

from(ROUTE_TO_MACE)
        .routeId(STUB_FILE_IMPORT_ROUTE_ID)
        .onException(Exception.class)
        .handled(false)
        .log(LoggingLevel.ERROR, STUB_IMPORT_ERROR_CODE)
        .end()
        .onException(IOException.class)
        .maximumRedeliveries(routesConfig.camelMaximumRetries).redeliveryDelay(routesConfig.camelRedeliveryDelay)
        .handled(false)
        .log(LoggingLevel.ERROR, STUB_IMPORT_ERROR_CODE)
        .end()
        .split().tokenizeXML(ITEM).streaming()
        .process(processor)
        .to("log:route.StubRoute?level=DEBUG")
        .end()
        .log("Stub file sucessfully processed:[${header.CamelFileName}]");

下面是单元测试:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class CamelRouteTest {
    @EndpointInject(uri = "mock:success_result")
    private MockEndpoint successResultEndpoint;

    @EndpointInject(uri = "direct:mock-import-stub-download")
    private FluentProducerTemplate producer;

    @Autowired
    private CamelContext camelContext;

    @MockBean
    RestTemplate restTemplate;


    private static final String MOCK_IMPORT_STUB_DOWNLOAD = "direct:mock-import-stub-download";
    private static final String TEST_STUB_FILE_LOCATION = "src/test/resources";

    @Before
    public void setup() throws Exception {
        camelContext.getRouteDefinition(STUB_FILE_DOWNLOAD_ROUTE_ID).autoStartup(true).adviceWith(camelContext,
                new AdviceWithRouteBuilder() {
                    @Override
                    public void configure() throws Exception {
                        replaceFromWith(MOCK_IMPORT_STUB_DOWNLOAD);
                        interceptSendToEndpoint("log:route.StubRoute?level=DEBUG").skipSendToOriginalEndpoint().to(successResultEndpoint);
                    }
                });

        camelContext.start();
    }

    @Test
    public void testFileDownloadRouter() throws Exception {
        File file = new File(TEST_STUB_FILE_LOCATION + "/Stub_11092018_162149_59642501.xml");
        successResultEndpoint.expectedMessageCount(1);
        producer.withBody(file).withHeader(Exchange.FILE_NAME, "Stub_24102018_162149_59642501.xml").send();
        successResultEndpoint.assertIsSatisfied();                                                                                                                                                                                                
    }

我总是将邮件计数设为0。这是错误

  

java.lang.AssertionError:mock:// success_result收到的消息   计数。预期:<1>,但为:<0>预期:<1>实际:<0>

我在这里做错了什么?如您所见,我有2条路线-第一条实际上到达了第二条路线,因此在单元测试中我也应该有2条路线吗?我没有添加2条路由,因为如果进行调试,我可以看到它实际上经过了处理器并返回了正确的结果。

1 个答案:

答案 0 :(得分:1)

首先:您正在使用 AdviceWith ,因此应在测试类上放置注释@UseAdviceWith。否则,骆驼上下文的自动启动和路线建议可能会重叠。

对于Mock上丢失的消息:也许您的测试断言为时过早。我猜生产者在处理消息时不会阻塞,但是MockEndpoint断言在发送消息后立即生效。发送消息后,接收到的消息数仍为0。

要检查等待是否有帮助,可以插入Thread.sleep()。如果可行,您应该摆脱Thread.sleep()并将其替换为Camel NotifyBuilder

又看到了另一点。 to()链中的最后一个interceptSendToEndpoint指向MockEndpoint实例变量。我认为这应该指向MockEndpoint URI,即.to("mock:success_result")

甚至还有一个:您通过getRouteDefinition(STUB_FILE_DOWNLOAD_ROUTE_ID)获得了第一条建议的途径,但是在此建议块中,您给出了两条途径的建议。那可能是您遇到问题的原因。不建议第二条路线,因此您的模拟游戏不到位。您必须在自己的建议块中为第二条路线提供建议。

@Before
public void setup() throws Exception {
    camelContext.getRouteDefinition(STUB_FILE_DOWNLOAD_ROUTE_ID).autoStartup(true).adviceWith(camelContext, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith(MOCK_IMPORT_STUB_DOWNLOAD);
        }
    });
    camelContext.getRouteDefinition(STUB_FILE_IMPORT_ROUTE_ID).autoStartup(true).adviceWith(camelContext, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("log:route.StubRoute?level=DEBUG").skipSendToOriginalEndpoint().to("mock:success_result");
        }
    });

    camelContext.start();
}