I'm trying to set up a test for a Camel route. My test route reads a binary file and sends it to a translator bean, returning a POJO. Now, I'd like to do some assertions on the POJO to ensure the values that are there match the known values. Standard stuff I think. In the examples I've seen, the body seems to always be a String or primitive type, and a simple assertion can be done on it. However, in my case, it is an object, so I want to get the object somehow.
Here's what I've tried so far:
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:///path/to/dir/of/test/file/?noop=true")
.bean(TranslatorBean.class)
.to("mock:test").end();
}
};
}
@Test
public void testReadMessage() throws Exception {
MockEndpoint endpoint = getMockEndpoint("mock:test");
endpoint.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Object body = exchange.getIn().getBody();
assertIsInstanceOf(MyPOJO.class, body);
MyPOJO message = (MyPOJO) body;
assertEquals("Some Field", someValue, message.getSomeField());
// etc., etc.
}
});
//If I don't put some sleep here it ends before anything happens
Thread.sleep(2000);
}
When I run this, it appears that it runs correctly, but when I step through, I can see an assertion fails. For some reason, this does not get reported.
So then I tried inlining my Processor in the route like so:
public void configure() throws Exception {
.from("file:///path/to/dir/of/test/file/?noop=true")
.bean(TranslatorBean.class)
.process(new Processor() {
//same code as before
});
}
This kind of works, but with a huge problem. Any assertions that fail still do not get reported by JUnit. Instead, they get caught within Camel, and reported as a CamelExecutionException with absolutely no information about the cause. Only by stepping through the debugger can you determine which assertion failed. Also, this way my entire test is within the configure method instead of in its own test method. I have to include an empty test with a sleep to get it to run. Clearly this is terrible, but I'm sure what the right thing to do here is. It looks like a Processor might not be the correct route, but I'm not seeing the correct way. Any guidance is much appreciated.