Automating Integration Testing for Apache Camel with Spring Boot

Ajanthan Eliyathamby 🇱🇰
4 min readFeb 25, 2024

This article is to explain about Automating the Integration Test of the flow in my previous article [1]. In the article [1] we have done the unit testing for the route using the Mock Endpoint option in Apache Camel, that will be fine for the unit testing purpose. But for integration testing of the specific route we need to actually verify whether the specific ActiveMQ instance is receiving the messages properly.

Earlier, we had Camel Test containers which help to do the integration testing using the containers. With the latest updates Apache Camel provides the library called test-infra, which help us to achieve the integration testing goal in a supportive and improved way. It already have list of modules that have the support for this. Refer [2] and [3] for more information on this.

So here, in this article I’m going to use camel-test-infra and Spring Boot capabilities to implement the Integration Test for the Route File to Topic.

Comparison of the Actual Message Flow, Unit Testing Flow and Integration Testing Flow

The above image is the summarized representation of the Flows that involved in each Main, Unit and Integration Test.

Main Integration Flow: This is the runtime flow, which will take the file from a directory, actually this will be a FTP location, but for the testing purpose, I’m using the location where the application is running. First our Apache Camel route will pick up this CSV file and process it and combine multiple rows into a single message and post that to the ActiveMQ Topic. So our intention now will be to do an automated unit test that will mainly focus the route is tested and make sure the route configuration is properly working. Then the next Integration test will be using an actual implementation of the ActiveMQ and make sure the Integration with a Test Environment is working well.

Unit Test: This one we covered in the [1], where we will load the route and make sure the route configuration is working fine and doing the transformation properly and passing the aggregated messages to a mock endpoint — where we will validate the message count with the mockEndpoint.setExpectedMessageCount(count);

Integration Test: This is the purpose of this article and what we are going to see is how we can make sure we are testing with an actual ActiveMQ instance and checking the messages that were received by the topic. Here we need to make sure that our implementation should support both the embedded or internally spinned up ActiveMQ instance and also by using configuration we can route to en external ActiveMQ instance.

  1. As an initial step add the below dependencies in the pom.xml

If you want to check what are the other test-infra implementations already available refer [6].

2. Then prepare a ActiveMQ Configuration Class, why this is need is based on the test-infra we can use the RegisterExtension annotation to start an embedded ActiveMQ Service, but the problem is that, if we want to control that for our implementation it is out of our hands. As in my use case i need to implement a test which can support both the Internal Embedded and also the external ActiveMQ instance, have come up with the below class which will be only triggered if the app.embedded property in application.yml is true.

3. Then prepare the actual test class. Below is my sample.

Here, you can observe that the routes were stopped at the start of the test. This is optional, why i done is to make sure my test is running only on the specific route im going to test. Because when the routes loaded in camel context, by default all routes will be enabled to start auto, so all the routes configured in the project will be active. Then the logic that comes to consume and make the count of the messages received.

Then under the @AfterEach, removed two files that will be generated during the test and finally added the cachingConnectionFactory.destroy();, this is because of the connection WARN error that will come when using the Embedded ActiveMQ instance.

4. Finally make sure the needed properties for the testing route is configured in the application-test.yml. Below is my sample.

That’s it.

Further, as the same approach how we done the ActiveMQ Test Infra, we can do that for the FTP as well using the [7]. Then we can make sure that the consuming part also using an actual internal embedded and External FTP instances. Will try to cover that in my next blog.

References

[1] https://medium.com/@ajanthan-eliyathamby/efficient-large-file-processing-with-apache-camel-part-2-848a04674509

[2] https://camel.apache.org/blog/2022/02/camel-test-infra/

[3] https://camel.apache.org/manual/test-infra.html

[4] https://www.youtube.com/watch?v=RbVe3cFH37w&t=734s

[5] https://github.com/apache/camel/tree/camel-3.20.4/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services

[6] https://github.com/apache/camel/tree/camel-3.20.4/test-infra

[7] https://github.com/apache/camel/tree/camel-3.20.4/test-infra/camel-test-infra-ftp

--

--

Ajanthan Eliyathamby 🇱🇰

Associate Architect — Enterprise Integration | 14x WSO2 | 1x HashiCorp | 1× Azure | Runner-Up WCPY 2020 | https://ajanthane.blogspot.com