Apache Camel | Spring Testing | Intercept Route is not working

705 Views Asked by At

I am new to apache camel so I am still struggling to write camel test cases.

I have below route defined

from("direct:routeToTest")
        .id(ROUTE_ID)
        .to(LOOK_UP_ROUTE)
        .choice()
           .when(some-condition)        
             .choice()
                .when(condition)
                .to(CREATE_ROUTE)
               .otherwise().process(exchange -> exchange.getIn().setBody(prepareResponse(""))
             .endChoice()
          .otherwise()
            .log("Some Issue")
            .process(exchange -> unknownError(exchange))
        .endChoice();
  }

while testing I am trying to intercept to defined in my route and set some mock response to it. So after some search I found using adviceWith is the right way to achieve it.

So my test is like below. The outcome of the test is, it is still going to Look_up_route (direct:lookUpRoute, another route defined) for processing with the data passed but the expectation is code should skip this to and set response as "MockResponse"

@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@RunWith(CamelSpringBootRunner.class)
@UseAdviceWith
@MockEndpoints
@DisableJmx(false)
public class RouteTest {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private CamelContext context;

 @Test
  public void testResponseToJSON() throws Exception {
    SomeObject someObject = getObject();
    context.getRouteDefinition(ROUTE_ID).adviceWith(context, new AdviceWithRouteBuilder() {
          @Override
          public void configure() throws Exception {
            interceptSendToEndpoint(LOOK_UP_ROUTE)
                .skipSendToOriginalEndpoint()
                .transform("MockOutput");
        }
    );
    context.start();
    Object object = producerTemplate.requestBody(direct:routeToTest, someObject);
  }

}

I'd like to know how do I skip .to(LOOK_UP_ROUTE) and set mockResponse to it.

1

There are 1 best solutions below

0
On

Your intercept statement looks good at first sight. However, have a look at this answer for an alternative approach.

You have to

  1. Add an id to the LOOK_UP_ROUTE step in your route
  2. Then you can use adviceWith to remove or manipulate the marked step in your tests
  3. Set the message body (and header) to whatever it should be for your test