I’m trying to write my first code with Apache Camel right now. I try to follow the examples from Camel in Action, but I want to use my own example data.
What I want to do
Right now I want to read from a CSV file and get each line as a java bean.
Here is my junit test:
@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");
}
};
}
The CSV contains this:
row 01,row 02,,row 04
row 11, row 12, row 13, row 14
And this is my 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;
}
}
My Problem
When I run this test, the context is started and the route is loaded. But nothing is coming through. After about 10s the context is automatically stopped and my test fails. This is the stacktrace:
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)
Need help with
I guess I’m missing something obvious, maybe something that has to do with the test setup and not so much with my CsvRecord or my route. Can you give me a tip or maybe an URL to a better tutorial? The book is not very helpful at this point… 🙁
Again, right after posting my question, I found the answer myself. 😉 Here is a working junit test:
The unexpected thing for me is that I get a
Listfrom my endpoint route which in turn holds manyMaps. Each map has a keymy.package.MyBeanClasswith the value set to the actual unmarshalled row from my CSV file.