我需要测试我的骆驼异常处理程序:
@Configuration
public class RouteConfiguration extends RouteBuilder {
@Override
public void configure() throws Exception {
onException(HttpOperationFailedException.class).
handled(true).
log("HttpOperationFailedException: ${exception}").
onExceptionOccurred(myRestExceptionProcessor).id("myRestExceptionProcessor").end();
from("direct:myPrettyRoute").routeId("myPrettyRoute");//lots of routing here
}
}
我正在尝试在myRestExceptionProcessor之后添加adviceWith,但找不到方法。
public class MyExceptionRoutingTest {
@Autowired
private CamelContext context;
@Before
public void before() throws Exception {
if (ServiceStatus.Stopped.equals(context.getStatus())) {
log.info("prepare mocks endpoint");
List<OnExceptionDefinition> ed = context.getErrorHandlerBuilder().getErrorHandlers(context.getRoutes().get(0).getRouteContext());
//FAILS, because context.getRoutes() is empty at the moment
//even if it wasn't, getErrorHandlerBuilder() is deprecated
}
}
}
我需要为exceptionHandler定义添加以下内容:
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("myExceptionProcessor").after().to(myResultEndpoint).id("myResponseEndpoint");
}
});
有可能吗?
答案 0 :(得分:1)
我不完全了解您是否要测试错误处理程序(onException
块)还是仅测试myRestExceptionProcessor
,但是从骆驼的角度来看,这是两种测试:
因此,如果要在发生错误时测试路由,请在路由测试中抛出所需的错误,以触发错误处理程序。如果使用像Spring这样的依赖注入框架,这很容易,因为您可以注入引发错误的测试Bean,而不是路由中使用的实际Bean。
要在路线的末尾添加Mock端点,请使用adviceWith
.adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast().to("mock:error");
}
}
希望这会有所帮助。随意扩展您的问题,以进一步阐述您的问题。
答案 1 :(得分:0)
我已解决以下问题,无需更改路线:
//entry point of the route is invoked here
Exchange send = myProducer.withBody("body is here").send();
HttpOperationFailedException exception = send.getException(HttpOperationFailedException.class);
String responseBody = exception.getResponseBody();
//recieved result and made assertions
assert responseBody != null; // any other assertions