对象结果在Apache Camel junit测试中

时间:2018-02-16 06:47:24

标签: java junit apache-camel

我尝试将camel输出测试为Object但无法获得交换对象。这是失败的地方Customer resultCustomer = processActs.getExchanges().get(0).getIn().getBody(Customer.class)。请帮我解决这个问题。我提到了Right way to test my object in Camel

客户POJO:

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

测试路线:

public class FileTest4 extends CamelTestSupport {

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

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

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

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

    @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:processActs");

                from("direct:processActs").process(exchange -> {
                    List<Customer> customers = new ArrayList<>();
                    customers.add(new Customer("F1", "L1"));
                    customers.add(new Customer("F2", "L2"));
                    customers.add(new Customer("F3", "L3"));
                    exchange.getOut().setBody(customers);
                }).to("direct:write2File");

                from("direct:write2File").split(simple("${body}")).log("Content: ${body}");
            }
        };
    }

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

    @Test
    public void testUnmarshal() throws Exception {
        mockEndResult.expectedMessageCount(1);

    // ArrayIndex Exception here exchanges list is empty
        Customer resultCustomer = processActs.getExchanges().get(0).getIn().getBody(Customer.class); 
        assertEquals(resultCustomer.toString(),"F1:::L1"); 

        write2File.expectedBodiesReceived("F1:::L1", "F3:::L3", "F2:::L2");
        template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));
        mockEndResult.assertIsSatisfied();

    }

}

1 个答案:

答案 0 :(得分:1)

在您实际发送任何交换之前,您似乎正在检查模拟端点。尝试将支票移至测试结束,例如:

@Test
public void testUnmarshal() throws Exception {
    mockEndResult.expectedMessageCount(1);

    write2File.expectedBodiesReceived("F1:::L1", "F3:::L3", "F2:::L2");
    template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));
    mockEndResult.assertIsSatisfied();

    Customer resultCustomer = processActs.getExchanges().get(0).getIn().getBody(Customer.class); 
    assertEquals(resultCustomer.toString(),"F1:::L1"); 
}

<强>更新

经过仔细检查,我认为你的嘲笑已经搞砸了。根据断言判断,您要检查三个客户是否已写出来。但是你的嘲笑并没有为此设置。

mock:end已添加到MY_ROUTE的末尾,但只会看到处理器在direct:processActs

中返回的整个客户列表

您声明@EndpointInject的嘲讽也不参与此路线,因为您实际上并未模拟真实的终点。您可以删除mockEndResult以外的所有内容。

以下测试确实通过了。

@Test
public void testUnmarshal() throws Exception {
    mockEndResult.expectedMessageCount(1);

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

    mockEndResult.assertIsSatisfied();

    @SuppressWarnings("unchecked")
    List<Customer> customers = mockEndResult.getExchanges().get(0).getIn().getBody(List.class);
    assertEquals(customers.get(0).toString(), "F1:::L1");
    assertEquals(customers.get(1).toString(), "F2:::L2");
    assertEquals(customers.get(2).toString(), "F3:::L3");
}

但这可能不是你想要测试的。相反,您可以将模拟端点编织到拆分器中,然后您就可以断言个别客户。

@Override
protected void doPostSetup() throws Exception {
    context.getRouteDefinition("MY_ROUTE").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith("direct:teststart");
        }
    });
    // give direct:write2File the id 'splitter' to be able to advice it
    context.getRouteDefinition("splitter").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            weaveByType(LogDefinition.class).after().to("mock:end");
        }
    });
    context.start();
}

@Test
public void testUnmarshal() throws Exception {
    mockEndResult.expectedMessageCount(3);
    mockEndResult.expectedBodiesReceived("F1:::L1", "F2:::L2", "F3:::L3");

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

    mockEndResult.assertIsSatisfied();
}