DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • That Can Not Be Tested!: Spring Cache and Retry
  • Effective Engineering Feedback: Software Testing

Trending

  • Build Self-Managing Data Pipelines With an LLM Agent
  • Scaling Cloud Data Automation: A Practical Guide to Open Table Formats
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  1. DZone
  2. Coding
  3. Frameworks
  4. Camel Route - Unit Testing

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.

By 
Vignesh Prabhu user avatar
Vignesh Prabhu
·
Feb. 05, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
12.1K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.

Java
 




x
16


 
1
package com.demo.camelspringboot.route;
2

          
3
import org.apache.camel.builder.RouteBuilder;
4
import org.springframework.stereotype.Component;
5

          
6
@Component
7
public class CamelRoute extends RouteBuilder {
8
    @Override
9
    public void configure() throws Exception {
10
        from("{{routeStart}}")
11
                .log("Timer has been invoked")
12
                .pollEnrich("{{routeFrom}}")
13
                .to("{{routeTo}}");
14
    }
15
}
16

          



The route configurations are externalized and maintained in the application.yml file.

YAML
 




x


 
1
server:
2
  port: 8081
3

          
4
routeStart: timer:dev?period=5s
5
routeFrom: file:dev/input?delete=true
6
routeTo: file:dev/output
7
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.

dev input output dev.txt

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:

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>org.apache.camel</groupId>
3
    <artifactId>camel-test-spring</artifactId>
4
    <version>3.7.0</version>
5
    <scope>test</scope>
6
</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 Spring ApplicationContext 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:

Java
 




xxxxxxxxxx
1
37


 
1
package com.demo.camelspringboot.route;
2

          
3
import org.apache.camel.Exchange;
4
import org.apache.camel.ProducerTemplate;
5
import org.junit.Test;
6
import org.junit.runner.RunWith;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.boot.test.context.SpringBootTest;
9
import org.springframework.core.env.Environment;
10
import org.springframework.test.context.ActiveProfiles;
11
import org.springframework.test.context.junit4.SpringRunner;
12
import java.io.File;
13
import static org.junit.Assert.assertTrue;
14

          
15
@ActiveProfiles("dev")
16
@SpringBootTest
17
@RunWith(SpringRunner.class)
18
public class RouteTest {
19

          
20
    @Autowired
21
    ProducerTemplate producerTemplate;
22
    @Autowired
23
    Environment environment;
24

          
25
    @Test
26
    public void testFileMovement() throws InterruptedException {
27
        String targetEnvironment=environment.getProperty("targetEnvironment");
28
        String fileContent="This is a file for "+targetEnvironment;
29
        String fileName=targetEnvironment+".txt";
30
        producerTemplate.sendBodyAndHeader(environment.getProperty("routeFrom"),fileContent, Exchange.FILE_NAME,fileName);
31
        Thread.sleep(5000);
32
        File outputFile=new File(targetEnvironment+"/output/"+fileName);
33
        assertTrue(outputFile.exists());
34
    }
35
}



Run the test and observe the successful execution of the test case.

Successful execution of the test case


Notice that the file is deleted from the input directory and copied to the output directory on successful execution.

successful execution

unit test Spring Framework Directory

Opinions expressed by DZone contributors are their own.

Related

  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • That Can Not Be Tested!: Spring Cache and Retry
  • Effective Engineering Feedback: Software Testing

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook