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
Newsletter
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
  • Testing Business Programs Without Constructing Domain Objects

Trending

  • As AI Agents Multiply, Repository Access Could Become an Infrastructure Problem
  • Running an OpenStreetMap Tile Server for the Whole Planet on Modest Hardware
  • Should an AI Agent Have Write Access to Your AWS Account?
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 05, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Testing Business Programs Without Constructing Domain Objects

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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