测试Apache Camel Junit的所有进程和路由

时间:2018-02-19 15:45:53

标签: java junit apache-camel

我正在尝试验证路线的所有过程,但是在它开始破坏后开始工作正常。请给我一些有用的参考或样品。谢谢。提前谢谢。

的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     at com.tgt.frs.collections.trm.ceds.routes.FileTest7 $ 1.lambda $ 0(FileTest7.java:61)     at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)     at 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)

1 个答案:

答案 0 :(得分:0)

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

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