如何使用Apache Camel和Bindy将CSV解组为Bean?

| 我现在正在尝试用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?这本书目前还不是很有帮助... :-(     
已邀请:
        同样,在发布问题后,我自己找到了答案。 ;-)这是一个有效的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\");
        }
    };
}
}
对我来说,意想不到的是,我从端点路由中得到了5英镑,而这又持有许多6英镑。每张地图都有一个键“ 7”,其值设置为我的CSV文件中实际未编组的行。     

要回复问题请先登录注册