Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

Camel is an integration framework that helps you build integration projects. It consists of a routing engine builder that provides the capability of building custom rules for routing. This allows you to decide upon the message sources and formats, to process these messages according to set rules, and then to dispatch these messages to their respective destinations. Camel allows you to set up data routing rules that are similar to business processes. Furthermore, Camel imposes no restrictions on data types or transfer protocols, allowing them to work with both standard and custom data types. The engine currently supports more than eighty data types or protocols, and the list is being constantly extended.

Camel includes enterprise integration patterns to solve most of your integration problems. Its domain specific language integrates with popular programming languages such as Java, Spring, and Scala. Additionally, Camel includes an automatic type converter, it has a lightweight core, and it provides a Test Kit to easily test Camel-based applications.

Setting up Apache Camel

The latest version of Camel can be downloaded from their official website. You can download the ZIP or TAR package depending upon your operating system. Simply extract the contents of the package, which contain the Camel library files along with some documentation and examples. The Camel projects are easily built using the Apache Maven archetype of preconfigured templates. Maven Eclipse can also be used to convert your Maven-based project into an Eclipse project. This gives you the advantage of using the Eclipse IDE for Camel development. The plug-in is executed using the following command in the project’s root directory:

Consider the following example that uses CamelContext to move a file from the input folder to the output folder:

In the example above, Camel takes a file from the input directory and moves it to the output directory. The noop argument will tell Camel to leave the file unmodified.

Now that you have downloaded Camel and we have walked through a simple example, let’s move to the main topic of this article, which is to introduce the test kit in Camel and its several uses.

Using Test Kit in Camel

Testing is a primary component of application development, and Camel offers a test kit that can be used to catch possible program errors and to simulate errors to note program behavior. The Camel test kit is built on top of JUnit, the standard Java Testing Unit, leveraging the ease of use and compatibility already available with JUnit. Read more about Mastering JUnit in JUnit in Action, Second Edition.

The Camel Unit Testing is actually a bundle of six classes that are extensions over the standard JUnit contained in the Camel-test.jar file. The classes are:

  • org.apache.Camel.test.TestSupport
  • org.apache.Camel.test.CamelTestSupport
  • org.apache.Camel.test.CamelSpringTestSupport
  • org.apache.Camel.test.junit4.TestSupport
  • org.apache.Camel.test.junit4.CamelTestSupport
  • org.apache.Camel.test.junit4.CamelSpringTestSupport

The choice of the class used is based upon the type of testing needed.

Mock Component for Camel Testing

The Mock component in Camel is used to simulate the behavior of your code in real situations. Hence it ‘mocks’ the actual scenario in which the code will run. It is similar to jMock and becomes very useful in conditions where it is not possible to test with real components; for example if you are developing just one of the several components and the other components are not reachable at that moment. It can also be used to simulate conditions, such as network transfer faults, and allows you to test things like payload verification and delivery order. Read more about the Mock component in Chapter 6.2. Using the Mock component in Camel in Action.

Consider this simple example of a Mock endpoint followed by some explanations:

If you need to mock existing endpoints of routes, use the getMockEndPoint method from the CamelTestSupport class. In order to verify the results, use the assertIsSatisfied() method, as shown in the previous example. The following code will show how to mock endpoints in a code sample:

Run the test now and verify if the given message is received at the output. Camel will throw an assertion error with a failure message in case of an exception. You can use the same methodology to test every major component in the code to see if they continue giving the correct output. Of course, the desired output will change depending upon the endpoints and portion of the code. The classes mentioned above provide support for most common types of components.

Mocks for Simulating Errors

A second type of testing is to verify the behavior of your program in case an error happens. This is to avoid any crash, and to ensure that the program continues working in case an error is generated due to any reason. Some components may not require such testing, such as when a component can only take a specified number of inputs. However, some components, such as user-fillable forms may cause exceptions. It is critical to handle them correctly.

The following example shows how to mock a connection error. In the example, you will need to check an FTP mock endpoint. The custom processor defined in the example will throw a connection exception:

In order to master Camel testing, I recommend trying out the mocks with various components that are defined in the testing classes. Take a look at the books in Safari Books Online below for further examples.

Safari Books Online has the content you need

Camel in Action is a Camel tutorial full of small examples showing how to work with the integration patterns. It starts with core concepts like sending, receiving, routing, and transforming data. It then shows you the entire lifecycle and goes into depth on how to test, deal with errors, scale, deploy, and even monitor your app — details you can find only in the Camel code itself.
JUnit in Action, Second Edition is an up-to-date guide to unit testing Java applications (including Java EE applications) using the JUnit framework and its extensions. This book provides techniques for solving real-world problems such as testing AJAX applications, using mocks to achieve testing isolation, in-container testing for Java EE and database applications, and test automation.

About the author

Usman Aziz is a technical lead at TunaCode, Inc., a startup that delivers GPU-accelerated computing solutions to time-critical application domains. He holds a degree in Computer Systems Engineering. His current focus is on protecting bulk data. He can be reached at usman@tunacode.com.

Tags: Apache Camel, Camel, Camel Unit Testing, Eclipse, java, JUnit, Mocks, Scala, spring, Test Kit,

Comments are closed.