如何使用Apache Camel和Bindy从CSV解组到Bean?

时间:2011-06-23 18:23:59

标签: unit-testing apache-camel

我正在尝试用Apache Camel编写我的第一个代码。我尝试按照Camel in Action中的示例进行操作,但我想使用自己的示例数据。

我想做什么

现在我想从CSV文件中读取并将每一行作为java bean。

这是我的junit测试:

@Test
public void testCsvWithBindy() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:queue.csv");
    mock.expectedMessageCount(2);

    assertMockEndpointsSatisfied();

    CsvBean line1 = mock.getReceivedExchanges().get(0).getIn()
            .getBody(CsvBean.class);
    assertEquals("row 01", line1.getFirst());
}

public RouteBuilder createRoute() {
    return new RouteBuilder() {
        public void configure() throws Exception {
            context.setTracing(true);

            from("file://src/test/resources?noop=true&fileName=test.csv")
                .unmarshal().bindy(BindyType.Csv, "my.package.for.csvrecord")
                .to("mock:queue.csv");
        }
    };
}

CSV包含以下内容:

row 01,row 02,,row 04
row 11, row 12, row 13, row 14

这是我的CsvRecord:

@CsvRecord(separator = ",")
public class CsvBean {
@DataField(pos = 1)
private String first;
@DataField(pos = 2)
private String second;
@DataField(pos = 3)
private String third;
@DataField(pos = 4)
private String fourth;

public String getFirst() {
    return first;
}

public void setFirst(String first) {
    this.first = first;
}

public String getSecond() {
    return second;
}

public void setSecond(String second) {
    this.second = second;
}

public String getThird() {
    return third;
}

public void setThird(String third) {
    this.third = third;
}

public String getFourth() {
    return fourth;
}

public void setFourth(String fourth) {
    this.fourth = fourth;
}
}

我的问题

当我运行此测试时,将启动上下文并加载路径。但没有任何事情发生。大约10秒后,上下文自动停止,我的测试失败。这是堆栈跟踪:

java.lang.AssertionError: mock://queue.csv Received message count. Expected: <2> but was: <0>
at org.apache.camel.component.mock.MockEndpoint.fail(MockEndpoint.java:1086)
at org.apache.camel.component.mock.MockEndpoint.assertEquals(MockEndpoint.java:1068)
at org.apache.camel.component.mock.MockEndpoint.doAssertIsSatisfied(MockEndpoint.java:367)
at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:346)
at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:334)
at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:172)
at org.apache.camel.test.junit4.CamelTestSupport.assertMockEndpointsSatisfied(CamelTestSupport.java:391)
at my.package.for.unittests.CsvToBeanWithBindyTest.testCsvWithBindy(CsvToBeanWithBindyTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

需要帮助

我想我错过了一些明显的东西,可能与测试设置有关,而不是我的CsvRecord或我的路线。你能给我一个提示或者一个更好的教程的URL吗?这本书在这一点上不是很有用......: - (

1 个答案:

答案 0 :(得分:3)

再次,在发布我的问题之后,我自己找到了答案。 ;-)这是一个有效的junit测试:

public class CsvToBeanWithBindyTest extends CamelTestSupport {
@Test
public void testCsv() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:queue.csv");
    mock.expectedMessageCount(1);

    assertMockEndpointsSatisfied();

    List line1 = (List) mock.getReceivedExchanges().get(0).getIn()
            .getBody();
    Map map1 = (Map) line1.get(0);
    CsvBean csv1 = (CsvBean) map1.get("my.package.CsvBean");
    assertEquals("row 01", csv1.getFirst());

    Map map2 = (Map) line1.get(1);
    CsvBean csv2 = (CsvBean) map2.get("my.package.CsvBean");
    assertEquals("row 11", csv2.getFirst());
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            context.setTracing(true);

            from("file://src/test/resources?noop=true&fileName=test.csv")
                .unmarshal(new BindyCsvDataFormat("my.package"))
                .to("mock:queue.csv");
        }
    };
}
}

对我来说意想不到的事情是我从我的终端路线获得List,而后者又拥有许多Map s。每张地图都有一个键my.package.MyBeanClass,其值设置为我的CSV文件中的实际未编组行。

相关问题