Test All Process and Routes of Apache Camel Junit

↘锁芯ラ 提交于 2019-12-11 16:56:51

问题


I am trying to validate all the processes of route but start is working fine after that it started to break.Please give me some useful reference or sample for this.Thanks in advance.

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 and Route:

    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();
        }

    }

Exception:Updated

Caused by: 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) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)


回答1:


The CamelContext is not automatically started by the test framework because you are using AdviceWith, to allow you to add advice to routes before they start. Add a call to startCamelContext() at the end of doPostSetup after you've added the advice.

Also, append doesn't appear to be a valid property of the file component so you'll need to remove append=true.



来源:https://stackoverflow.com/questions/48869548/test-all-process-and-routes-of-apache-camel-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!