Camel, Spring Boot, Gradle and Spock

 This post will walk through creating a simple camel route that will retrieve weather results from a SOAP web-service for cities contained in a CSV file and write the responses out to a file.

At the core of the this walk through is Camel, a lightweight middleware library that will orchestrate reading the files and initiating the web service calls and this will be hosted within a Spring Boot container that will manage much of the boilerplate logic leaving us to code! To build our code and pull down the dependencies, we are going to use Gradle as our build tool and finally we will have Spock give our application the test coverage we need.

You can find the complete code here;

The libraries we will use;

Let’s get started;

Gradle (build.gradle);

To build our weather example, we will use Gradle which will handle all the dependencies and the work tasks to build the Java artifacts for this example.

Bootstrap Class (weather.Application)

As we are using a Spring Boot container, we need to bootstrap the container and Camel to start when the main method is called. On Launch, Spring Boot will automatically configure our container and search for any routes that we have marked with @Component (and for us, this is only the WeatherRoute class).

 Camel Route (weather.route.WeatherRoute)

The Weather route is split into three sub routes; The first route reads the CSV file and splits each of the rows; the second route takes each of the values from the CSV file and applies them to the SOAP message contained within a Velocity template and the final route sends out our SOAP message to the Web Service and routes the response.

CSV Modelling (weather.model.WeatherModel)

For the CSV modelling, we use Bindy which handles CSV to Java Object marshalling and this plugs seamlessly into camel which will in turn send each of the rows from the CSV downstream. Camel handles this like so;

.unmarshal().bindy(BindyType.Csv, WeatherModel.class)

Calling the SOAP Service from our Route

There are multiple ways to interact with SOAP Web Service with the key libraries being CXF or Spring WS which Camel supports out of the box. While these do the job, I’m not a fan as you normally spend more time on configuring and fighting the libraries than you actually do on the domain functions you desire.

This walk through takes a much simpler approach by using a velocity template (template/weather/getWeatherRequest.vmfor the weather web-service SOAP message and inject the values from the CSV file into the template.

//enrich with velocity.

Once we have enriched the Velocity template, we pass it onto the sub-route that calls the SOAP web service.

//soap ws call


Property Files

By default, spring boot will use which contains the properties that gets injected into the route, properties such as the Soap Endpoint address, where the CSV file is located and what to do with the response from the web service.



In this example, we are using Groovy and Spock to write both our Unit & Integration tests. For those who have yet to play with Spock, I recommend this over JUnit as not only is it based on a BDD paradigm but Spock uses Groovy so we can write more concise and expressive tests.

 Tests – Unit (weather.CallMockedWebServiceSpecification)

 To unit test our sample weather application, we will use camels testing capabilities to inject mocks into the route as endpoints and validate that these endpoints are called as expected. To do this, we load a ‘’ file instead of the default ‘’ and this contains each of the mock endpoint values that we need.

In the test itself, we use the camel annotations that will give us the ability to mock out the endpoints so that we can be sure that all the calls came as expected.

 @Produce – Allows for injected message content into an endpoint at the start of the route as defined in the in the ‘’.

@EndpointInject – injects the mock Endpoint defined in the ‘’.

Tests – Integration (weather.CallRealWebServiceIntegrationTestSpecification)

To expand on the unit tests, we will create an Integration Test that will copy a CSV file to a temporary directory, have the camel route pick up this file and call an embedded web service simulator to simulate our real world http call. For this test, we are using the ‘’ properties file to override the default weather endpoint with our own endpoint location (the WSSimulator library) so we don’t have a dependency on external services within our tests

Executing the code

 To execute each of the tests you simply run the following commands from your shell (denote the .bat which you only append on windows environments)

  • Start the container & listen for the CSV file; ./gradlew.bat build && java -jar ./build/libs/camel-weather-1.0-SNAPSHOT.jar
  • To run a single test run e.g.; ./gradlew.bat -Dtest.single=CallRealWebServiceIntegrationTestSpecification test
  • Or you can run the tests directly from your IDE.

To summarise, we now have a self-contained, Spring aware Camel route that picks up values from a CSV file, calls an external SOAP service and writes the results to a file. Not bad for a few dozen lines of code! Enjoy.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s