I have two very simple routes (Code below is not the original classes, I have simplified it like removing setters, logs and so on)
The first route:
public static final String MESSAGE_CONSUMER = "vm:myMessage";
public String myXmlProducer; //and getter method then
myXmlProducer = "file:myFileLocation?autoCreate=true&fileName="+ myFileName;
from(MESSAGE_CONSUMER)
//some process here
.to(myXmlProducer);
and the second one:
public String fileConsumer = "file:myFileLocation";
public String ftpProducer = "ftp://ftpIp?username=username&password=password&maximumReconnectAttempts=0";
from(fileConsumer)
.to(ftpProducer );
I am trying to write some test for these two routes, here is some part of my test class,
configuration part in class:
public static final String MOCK_OUT = "mock:out";
public static final String DIRECT_IN = "direct:in";
public static final String MOCK_XML_URI = "mock:xmlFile";
...
@EndpointInject(uri = MOCK_OUT)
MockEndpoint mockOut;
@EndpointInject(uri = MOCK_XML_URI)
MockEndpoint mockXmlUri;
...
@Override
public RouteBuilder[] createRouteBuilders() {
final MyRoute route = new MyRoute();
route.setFtpProducer (MOCK_OUT);
RouteBuilder testHarness = new RouteBuilder() {
@Override
public void configure() throws Exception {
from(DIRECT_IN).routeId("testHarness.in")
.to(MyRoute.MESSAGE_CONSUMER)
from(route.getMyXmlProducer())
.convertBodyTo(Byte.class)
.to(mockXmlUri)
}
};
return new RouteBuilder[] {route, testHarness};
}
@Before
public void getTestData() throws IOException {
testInputData = IOUtils.toString(this.getClass().getResourceAsStream("/myTarget/inputData.txt"));
}
This test method is green:
@Test
public void testRoutingOk() throws InterruptedException {
mockOut.setExpectedMessageCount(1);
mockOut.message(0).body().isEqualTo(testInputData);
template.sendBody("file:myTestFileLocation", testInputData);
assertMockEndpointsSatisfied();
}
The problem is the following test, I am quiet new in camel, and have googled to find why, but cannot figure it out by myself:
@Test
public void testRoutingOk() throws InterruptedException {
mockXmlUri.setExpectedMessageCount(1);
mockXmlUri.message(0).body().isEqualTo(testInputData);
template.sendBody(DIRECT_IN, testInputData);
assertMockEndpointsSatisfied();
}
I get assetion error, no message at all:
java.lang.AssertionError: mock://xmlFile Received message count. Expected: <1> but was: <0>
Is there anyone who can help me, please.
After some days struggling, I found what the problem was. I am sending a message to body but, this message will be used by another route. I am using version 2.10, the solution to have a green unit test is to stop the route. With current version (2.10) it will be a little bit complicated, since I should change my main classes just for unit tests sake. But I can use controlbus: instead, if we upgrade our camel to 2.11+.