Marian Klühspies
Marian Klühspies

Reputation: 17707

How to mock Apache Camel Route before CamelContext is started in CamelSpringBootTest

Maybe a rather uncommon issue, but I'd like to mock certain Camel routes, in the JUnit setup method

@BeforeEach
void setUp() {
}

before the

@Override
public void configure() throws Exception {

}

is executed, which seems to happen, as soon as the CamelContext is autowired

@Autowired
protected CamelContext camelContext;

What do I need to do, to prevent autostarting of the Camel Context, in order to be able to mock my routes in the setup method and start it manually?

Upvotes: 0

Views: 982

Answers (2)

Meziane
Meziane

Reputation: 1667

This post is old but I don't like unanswered questions. So declare a MockEnpoint like so:

@EndpointInject("mock:mockExampleQueue")
MockEndpoint mockExampleQueue;

and add it to the routes at initialisation:

@BeforeAll
void setUpClass() throws Exception { 
  camelContext.addRoutes(
    new RouteBuilder() {
      @Override
      public void configure() {
        from("jms:queue:" + EXAMPLE_QUEUE)
          .to(mockExampleQueue);
        }
      });
  ..
}

EXAMPLE_QUEUE ist the queue you want to observe and that you must have definded in your code. Than make expectations:

mockExampleQueue.setExpectedCount(2);
mockExampleQueue.setResultWaitTime(20000);

and send a message or do what ever to let the routes do their expected jobs. Finally check that your expectations are fulfilled:

mockExampleQueue.assertIsSatisfied(); 

In an optional step you may check further details:

List<Exchange> receivedExchanges = mockExampleQueue.getReceivedExchanges();
...

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55540

You can enable advice with in the unit test - then you need to start Camel manually.

https://camel.apache.org/manual/advice-with.html#_enabling_advice_during_testing

Upvotes: 0

Related Questions