Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 在SpringBoot应用程序中为驼峰路由编写单元测试-获取messageCount 0_Spring Boot_Apache Camel_Camel Test - Fatal编程技术网

Spring boot 在SpringBoot应用程序中为驼峰路由编写单元测试-获取messageCount 0

Spring boot 在SpringBoot应用程序中为驼峰路由编写单元测试-获取messageCount 0,spring-boot,apache-camel,camel-test,Spring Boot,Apache Camel,Camel Test,我正在尝试为camel路由编写单元测试,它用于导入和处理文件 from(fullImportFTP) .routeId(STUB_FILE_DOWNLOAD_ROUTE_ID) .onException(Exception.class) .handled(false) .log(LoggingLevel.ERROR, STUB_IMPORT_ERROR_CODE) .end() .log("Proce

我正在尝试为camel路由编写单元测试,它用于导入和处理文件

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 收到的消息 计数预期:但过去:预期:实际:


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

首先:您使用的是,因此您应该在testclass上添加注释
@UseAdviceWith
。否则,驼峰上下文的自动启动和路由建议可能会重叠

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

要检查等待是否有帮助,可以插入一个线程。sleep()。如果它起作用,您应该去掉Thread.sleep()并用

我看到了另一点。
interceptSendToEndpoint
链中的最后一个
to()
指向MockEndpoint实例变量。我认为这应该指向mockEndpointURI,即
.to(“mock:success\u result”)

还有一个:您可以使用
getRoutedDefinition(存根文件\u下载\u路由\u 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();
}

我已经添加了@UseAdviceWith注释并进行了等待,但不幸的是,它仍然失败。将interceptEndPoint也更改为URI,再次失败。。不知道这是否有用,但我可以在日志中看到-“camel.component.mock.MockEndpoint:断言:mock://success_result 在路由关闭和错误消息之前。是的,在检查断言时会记录这一点。这并不意味着它得到了满足,就像“如果mock得到了满足,现在就断言…”,然后错误消息就是结果。只是为了隔离问题。您能否静态地用模拟端点替换log语句,即以拦截器应该做的方式更改代码并禁用拦截器?如果它能工作,那么拦截器就有问题了。如果仍然不起作用,则模拟端点不能正常工作。您必须使用块执行两个单独的adviceWith操作,请参见我的扩展答案