Ermintar
Ermintar

Reputation: 1393

Camel adviceWith for ExceptionHandler

I need to test my Camel Exception handler:

@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       

    }
}

I'm trying to add adviceWith after myRestExceptionProcessor, but can't find a way.

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
        }
    }
}

I need to add something like this for the exceptionHandler definition:

    .adviceWith(context, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                weaveById("myExceptionProcessor").after().to(myResultEndpoint).id("myResponseEndpoint");
            }
        });

Is it possible?

Upvotes: 0

Views: 3403

Answers (2)

Ermintar
Ermintar

Reputation: 1393

I've solved the trick as follows, without changing the route:

//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

Upvotes: 1

burki
burki

Reputation: 7005

I don't fully understand if you want to test your error handler (onException block) or just your myRestExceptionProcessor, but from a Camel perspective these are two kinds of tests:

  1. Routing-Tests to test your routing logic and make sure that messages are correctly routed under various conditions that could happen in the route. This is the kind of tests you write with the Camel Testkit (that offers adviceWith and much more).
  2. Classic unit tests to test an isolated Bean, Processor or anything else that is used in the route to implement business logic. This kind of test is done with JUnit, TestNG or other classic unit test frameworks, it has nothing to do with Camel. Do not try to test such components with Camel Route tests since it is much more complicated than in a unit test!

So, if you want to test your routing when an error occurs you throw the needed error in your route test to trigger the error handler. If you use a dependency injection framework like Spring this is easy since you can inject a test Bean that throws an error instead of a real Bean used in the route.

To add a Mock endpoint at the end of a route, use adviceWith

.adviceWith(camelContext, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        weaveAddLast().to("mock:error");
    }
}

Hope this helps a bit. Feel free to extend your question to elaborate your problem a bit more.

Upvotes: 2

Related Questions