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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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
  • Spring Boot - Unit Test your project architecture with ArchUnit

Trending

  • The End of “Good Enough Agile”
  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • Scaling Microservices With Docker and Kubernetes on Production
  • Rust, WASM, and Edge: Next-Level Performance
  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
11.8K 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
  • Spring Boot - Unit Test your project architecture with ArchUnit

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!