在启动上下文之前,您可以覆盖 startCamelContext
方法并使用 weaveById
或 weaveByToUri
替换有问题的端点,AdviceWithRouteBuilder
。
@Override
protected void startCamelContext() throws Exception {
context.getRouteDefinition("queryDBForTasks").adviceWith(context,new AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
weaveByToUri("jdbc:*")
.replace()
.to("mock:jdbc");
}
});
super.startCamelContext();
}
之后,在测试开始时,您可以使用 AdviceWithRouteBuilder
和 weave 方法来控制这些模拟端点的行为。
@Test
public void TestJDBCRoute() throws Exception {
context.getRouteDefinition("queryDBForTasks").adviceWith(context,new AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
weaveByToUri("mock:jdbc")
.after()
.setBody(constant(getTasksForTest()));
weaveAddLast()
.setProperty("testResultCount").simple("${body.size()}")
.to("mock:result");
}
});
MockEndpoint resultMockEndpoint = getMockEndpoint("mock:result");
resultMockEndpoint.expectedMessageCount(1);
resultMockEndpoint.message(0)
.exchangeProperty("testResultCount").isEqualTo(4);
template.sendBody("direct:queryDBForTasks",null);
resultMockEndpoint.assertIsSatisfied();
}
对于任何具有消费者端点(如计时器、cron 作业或需要 bean 或连接在测试期间不可用的端点)的有问题的路由,您可以使用 removeRouteDefinition
中的 startCamelContext
删除所述路由。
@Override
protected void startCamelContext() throws Exception {
context.removeRouteDefinition(
context.getRouteDefinition("someRoute"));
super.startCamelContext();
}
本文链接:https://www.f2er.com/1055.html