1

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

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 条路线,因为如果我进行调试,我可以看到它实际上通过了处理器并返回了正确的结果。

4

1 回答 1

1

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

对于 Mock 上缺少的信息:也许您的测试断言太早了。我猜生产者在处理消息时不会阻塞,但 MockEndpoint 断言在发送消息后立即跟随。发送消息后,收到的消息计数仍为 0。

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

刚刚看到另一个点。to()链中的 finalinterceptSendToEndpoint指向 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();
}
于 2018-11-01T07:45:57.710 回答