MockEndpoints not working in Camel 2.14 and Spring 4.07
I am trying to mock my camel endpoints in my unit tests. I am using Camel Enhanced Spring Test.
This is my unit class:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("development")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, FirstbirdTestExecutionListener.class,
FlywayTestExecutionListener.class })
@ContextConfiguration(locations = { "classpath:hibernate-spring.xml", "classpath:broker.xml",
"classpath:camel/camel-commons.xml",
"classpath:camel/firstbird-application-consumer.xml", })
@FlywayTest
@MockEndpoints("bean:applicationResponseProcessor")
@DisableJmx(false)
public class FirstBirdApplicationsTest extends AbstractJUnit4SpringContextTests {
private static final String resourceUri = "src/test/resources/data/firstbird/";
@Autowired
@Qualifier("context-firstbird-applications-consumer")
protected ModelCamelContext camelContext;
@Produce(uri = "direct:firstbird.application.service")
private ProducerTemplate in;
@EndpointInject(uri = "mock:out")
private MockEndpoint mockOut;
@EndpointInject(uri = "mock:bean:applicationResponseProcessor", context = "context-firstbird-applications-consumer")
private MockEndpoint mock;
@BeforeClass
public static void setup() {
FirstbirdJsonHelper.setResourceUri(resourceUri);
}
@Test
public void inComingApplicationsTest() throws Exception {
assertEquals(ServiceStatus.Started, camelContext.getStatus());
// not wirking!
camelContext.setTracing(true);
DefaultExchange exchange = new DefaultExchange(camelContext);
mock.expectedMessageCount(1);
mockOut.expectedMessageCount(1);
// not working
MockEndpoint test = MockEndpoint.resolve(camelContext, "mock:bean:applicationResponseProcessor");
camelContext.getRouteDefinition("firstbird-incomming-applications").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpoints("bean:applicationResponseProcessor");
}
});
exchange.getIn().setBody("GoGo Gadget Route me!");
in.send(exchange);
// Thread.sleep(4000);
// not working
List<Exchange> receivedExchanges = mock.getReceivedExchanges();
ApplicationResponse applicationResponse = (ApplicationResponse) receivedExchanges.get(0).getIn().getBody();
// working
mockOut.assertIsSatisfied();
}
}
This is the appropriate route:
<camelContext errorHandlerRef="loggingErrorHandler" id="context-firstbird-applications-consumer" trace="true" xmlns="http://camel.apache.org/schema/spring">
<route id="firstbird-incomming-applications">
<from id="firstbird-ws-producer" uri="{{firstbird.applications.service}}"/>
<log logName="firstbird.data.consumer.firstbird-ws-producer" loggingLevel="INFO" message="Send data to queue"/>
<setExchangePattern pattern="InOnly"/>
<recipientList id="webservice-recipientlist" ignoreInvalidEndpoints="true" stopOnException="false">
<simple>activemq:${header.operationName}</simple>
</recipientList>
<to uri="bean:applicationResponseProcessor"/>
<to uri="mock:out"/>
</route>
</camelContext>
Basically, I want to mock the bean: applicationResponseProcessor , so I created my MockEndpoint mock . The problem is that it doesn't seem to be introduced. I can still debug my applicationResponseProcessor bean.
The only solution that works is to use AdviceWith:
camelContext.getRouteDefinition("firstbird-incomming-applications").adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpoints("bean:applicationResponseProcessor");
}
});
Another problem I ran into was enabling tracking in my JUnit test. Enable tracing via
camelContext.setTracing(true);
Does not work. I have to explicitly set the tracing to my XML file.
+3
source to share
No one has answered this question yet
Check out similar questions: