Camel Route - Unit Testing
This article covers the unit testing of a SpringBoot Camel Route using Junit with code examples provided to further your understanding of these concepts.
Join the DZone community and get the full member experience.
Join For FreeThis article covers the unit testing of a SpringBoot Camel Route using Junit.
Let us consider the use case of a process that moves the file from the input directory to the output directory by polling at specific timer intervals.
package com.demo.camelspringboot.route;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
public class CamelRoute extends RouteBuilder {
public void configure() throws Exception {
from("{{routeStart}}")
.log("Timer has been invoked")
.pollEnrich("{{routeFrom}}")
.to("{{routeTo}}");
}
}
The route configurations are externalized and maintained in the application.yml file.
x
server
port8081
routeStart timer dev?period=5s
routeFrom file dev/input?delete=true
routeTo file dev/output
targetEnvironment dev
On successful run, notice that the file is deleted from the input directory of the dev folder and copied to the output directory as per the configuration.
Rather than checking manually if the file is copied to the output directory, a more ideal approach to test is by writing Junit test cases.
Add the below dependency to the pom.xml:
xxxxxxxxxx
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>3.7.0</version>
<scope>test</scope>
</dependency>
ProducerTemplate
object is used to produce the files and place in the input directory.@ActiveProfiles
annotation can be used to activate the spring profiles. In the below example, we will use dev profile.@SpringBootTest
annotation, Spring Boot provides a convenient way to start up an application context to be used in a test.- The
SpringRunner
provides support for loading a SpringApplicationContext
and having beans@Autowired
into your test instance
In the below example, we create the input file in the input directory and at the end of test execution, we check if the file is successfully moved to output directory:
xxxxxxxxxx
package com.demo.camelspringboot.route;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import static org.junit.Assert.assertTrue;
"dev") (
SpringRunner.class) (
public class RouteTest {
ProducerTemplate producerTemplate;
Environment environment;
public void testFileMovement() throws InterruptedException {
String targetEnvironment=environment.getProperty("targetEnvironment");
String fileContent="This is a file for "+targetEnvironment;
String fileName=targetEnvironment+".txt";
producerTemplate.sendBodyAndHeader(environment.getProperty("routeFrom"),fileContent, Exchange.FILE_NAME,fileName);
Thread.sleep(5000);
File outputFile=new File(targetEnvironment+"/output/"+fileName);
assertTrue(outputFile.exists());
}
}
Run the test and observe the successful execution of the test case.
Opinions expressed by DZone contributors are their own.
Comments