Reputation: 4388
I am trying to mock a Processor in a Camel test for a route containing
.bean("xxx")
.bean("messageParserProcessor")
.bean("yyy")
The test class (simplified):
public class SomeTest extends CamelTestSupport {
@EndpointInject(uri = "mock:messageParserProcessor")
protected MockEndpoint messageParserProcessorMock;
// Other declarations
@Override
public boolean isUseAdviceWith() {
return true;
}
@Before
public void setUpContext() throws Exception {
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("messageParserProcessor")
.skipSendToOriginalEndpoint()
.bean(getMockEndpoint("mock:messageParserProcessor")); // Same as using messageParserProcessorMock
}
});
}
@Test
public void testParser() throws Exception {
context.start();
String expectedBody = "test";
messageParserProcessorMock.expectedBodiesReceived(expectedBody);
ProducerTemplate template = context.createProducerTemplate();
template.sendBody(producerTemplateUri, expectedBody);
messageParserProcessorMock.assertIsSatisfied();
context.stop();
}
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
jndi.bind("messageParserProcessor", new MessageParserProcessor());
// Other bindings
return jndi;
}
// Other methods
}
When I run the test, no mock is used and I see in the logs that the actual MessageParserProcessor from the registry is used.
Here is a relevant part of the logs:
Skipping starting CamelContext as isUseAdviceWith is set to true.
AdviceWith route after:
Route[[From[aws-sqs://xxx]] ->
[InterceptSendToEndpoint[messageParserProcessor -> [Bean[mock://messageParserProcessor]]], Bean[ref:messageParserProcessor]]]
*Here I get logs from the actual processor, which I don't expect*
What is wrong in my setup? I also tried to do:
interceptSendToEndpoint("messageParserProcessor")
.skipSendToOriginalEndpoint()
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// Log and print something
}
});
But nothing is printed or logged.
Also I was wondering why I must bind actual beans in the first place in createRegistry()
knowing I want to use mocks? (I tried not to but I get errors). It's weird, it's like using a framework such as Mockito to mock objects that we should first create as actual objects...
Upvotes: 0
Views: 1320
Reputation: 3191
Why do you have .bean(getMockEndpoint("mock:messageParserProcessor"));
? Shouldn't it be .to("mock:messageParserProcessor")
I usually create mockendpoints like this:
@Before
public void setUp() throws Exception {
super.setUp();
context.getRouteDefinition("MyRoute").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("MyEndPointId").replace().to("mock:MyMockEndpoint");
}
});
Then in my @test method, after context.start()
I use the mockendpoint to assert something like:
getMockEndpoint("mock:MyMockEndpoint").expectedMessageCount(size);
Upvotes: 1