I am working on testing an Apache Camel route that utilizes a RoutePolicy. The route code is as follows:
from("direct:test")
.routeId(IMPORT_AGENTS_REFERENTIAL)
.noAutoStartup().shutdownRunningTask(ShutdownRunningTask.CompleteAllTasks)
.routePolicy(routeStartStopPolicy)
.setHeader(EsbExchangeProperty.EXTENSION, constant("test"))
.to("log:test2");
}
Here is my test class:
@CamelSpringBootTest
@EnableAutoConfiguration
@SpringBootTest(
properties = { "camel.springboot.name=customName" }
)
class Test{
@Autowired
ProducerTemplate producerTemplate;
@Autowired
CamelContext camelContext;
@Autowired
static RoutePolicy routeStartStopPolicy;
@EndpointInject("mock:log")
MockEndpoint mockEndpoint;
@Test
public void shouldAutowireProducerTemplate() {
assertNotNull(producerTemplate);
}
@Test
public void shouldSetCustomName() {
assertEquals("customName", producerTemplate.getCamelContext().getName());
}
@Test
public void shouldInjectEndpoint() throws Exception {
adviceWith(camelContext, "ROUTE 1", a -> {
a.replaceFromWith("direct:modifiedStart");
a.weaveByToUri("log:test2").replace().to("mock:log");
});
camelContext.start();
camelContext.getRouteController().startRoute("ROUTE 1");
mockEndpoint.expectedBodiesReceived("Hello 1");
mockEndpoint.setExpectedMessageCount(1);
mockEndpoint.expectedHeaderReceived(EsbExchangeProperty.EXTENSION,"json");
producerTemplate.sendBody("direct:modifiedStart", "Hello 1");
mockEndpoint.assertIsSatisfied();
}
}
I believe this issue is related to the RoutePolicy. If I comment out the routePolicy(routeStartStopPolicy) line in the route configuration, the test runs successfully.
I want to test the route without modifying the actual route code. How can I resolve this issue and properly inject a RoutePolicy for testing?.
I’ve tried to find a method in Apache Camel that allows me to mock the RoutePolicy, but I haven’t been able to find one.
hadokhdokh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.