【问题标题】:Test All Process and Routes of Apache Camel Junit测试Apache Camel Junit的所有进程和路由
【发布时间】:2018-07-29 21:18:23
【问题描述】:

我正在尝试验证路由的所有过程,但在它开始中断之后启动工作正常。请为此提供一些有用的参考或示例。提前致谢。

test.txt:

F1:L1
F2:L2
F3:L3

Customer.java

    private String firstName;
    private String lastName;
    // getters and setters
    @Override
    public String toString(){
        return firstName +":::" + lastName;
    }
}

JUnit 和路线:

    public class FileTest7 extends CamelTestSupport {

        @EndpointInject(uri = "direct:teststart")
        private Endpoint start;

        @EndpointInject(uri = "mock:direct:process1")
        private MockEndpoint mockProcess1;

        @EndpointInject(uri = "mock:direct:process2")
        private MockEndpoint mockProcess2;

        @EndpointInject(uri = "mock:direct:process3")
        private MockEndpoint mockProcess3;

        @EndpointInject(uri = "mock:direct:write2File")
        private MockEndpoint mockWrite2File;

        @EndpointInject(uri = "mock:end")
        private MockEndpoint mockEnd;

        @Override
        public boolean isUseAdviceWith() {
            return true;
        }

        @Override
        protected RouteBuilder createRouteBuilder() throws Exception {
            return new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("file:/var/file.log&noop=true").routeId("MY_ROUTE").to("direct:process1");

                    from("direct:process1").routeId("process1").process(exchange -> {
                        File file = exchange.getIn().getBody(File.class);
                        FileInputStream fis = new FileInputStream(file);
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                        List<Customer> customers = new ArrayList<>();

                        String line = null;
                        while ((line = br.readLine()) != null) {
                            String[] names = line.split(",");
                            customers.add(new Customer(names[0], names[1]));
                        }
                        br.close();
                        exchange.getOut().setBody(customers);
                    }).to("direct:process2");

                    from("direct:process2").routeId("process2").split(simple("${body}")).to("direct:process3");

                    from("direct:process3").routeId("process3").process(exchange -> {
                        Customer customer = exchange.getIn().getBody(Customer.class);
                        String content = "Content:" + customer.toString();
                        exchange.getIn().setBody(content);
                    }).to("direct:write2File");

                    //Below updated
from("direct:write2File").routeId("write2File").to("file:/src/test/resources?fileName=test_out.log&fileExist=Append");

                }
            };
        }

        @Override
        protected void doPostSetup() throws Exception {
            context.getRouteDefinition("MY_ROUTE").adviceWith(context, new AdviceWithRouteBuilder() {
                @Override
                public void configure() throws Exception {
                    replaceFromWith("direct:teststart");
                    weaveAddLast().to("mock:end");
                }
            });
            context.start();//updated
        }

        @Test
        public void testUnmarshal() throws Exception {

            template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));


            List<Customer> customers = mockProcess1.getExchanges().get(0).getIn().getBody(List.class);
            System.out.println("customers:"+customers.size());

            Customer customer1 = mockProcess2.getExchanges().get(0).getIn().getBody(Customer.class);
            System.out.println("customer1:"+customers.toString());


            String customer = mockProcess3.getExchanges().get(0).getIn().getBody(String.class);
            System.out.println("customer:"+customer);

            String customerString = mockWrite2File.getExchanges().get(0).getIn().getBody(String.class);
            System.out.println("Customer String:"+customerString);

            String customerFinal =  mockEnd.getExchanges().get(0).getIn().getBody(String.class);
            System.out.println("Customer String:"+customerFinal);


            assertMockEndpointsSatisfied();
        }

    }

例外:已更新

引起:java.lang.ArrayIndexOutOfBoundsException:1 在 com.tgt.frs.collections.trm.ceds.routes.FileTest7$1.lambda$0(FileTest7.java:61) 在 org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63) 在 org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:541) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:120) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:83) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) 在 org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) 在 org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)

【问题讨论】:

  • 正确的解决方案是order your routes' startup order,但是这个测试引出了这里实际测试什么的问题。您正在有效地模拟您的路线,而您只是在真正测试处理器。最好单独测试处理器,而不是嵌入这样的路径中,如果没有别的,可以更简单地设置测试。

标签: java junit apache-camel


【解决方案1】:

CamelContext 不会由测试框架自动启动,因为您使用的是 AdviceWith,以便您在路由开始之前添加建议。添加建议后,在doPostSetup 末尾添加对startCamelContext() 的调用。

另外,append 似乎不是file 组件的有效属性,因此您需要删除append=true

【讨论】:

  • 您的文件使用冒号作为分隔符,但您使用逗号分隔。来吧,阅读堆栈跟踪!调试你的代码!
猜你喜欢
  • 2017-07-05
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 2016-09-29
相关资源
最近更新 更多