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

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • Detecting Plan Regression in SQL Server Using Query Store
  • Metal and Skins
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Engineering Closed-Loop Graph-RAG Systems, Part 2: From Prompts to Rules
  1. DZone
  2. Coding
  3. Frameworks
  4. Camel Route: Mock Testing

Camel Route: Mock Testing

This article covers the mock testing of a SpringBoot Camel Route. Camel provides two components to perform mock testing.

By 
Vignesh Prabhu user avatar
Vignesh Prabhu
·
Feb. 07, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
16.2K Views

Join the DZone community and get the full member experience.

Join For Free

This article covers the mock testing of a SpringBoot Camel Route.

Mock testing deals with mocking the actual data.

Camel provides two components to perform mock testing:

  • Direct Component — which takes the input from the Junit test case:

          from(direct:input)

  • Mock Component — which sends the output to the Junit test case:

          to(mock:output)


ProducerTemplate object is used to produce the files and place them in the input directory.

@ActiveProfiles annotation can be used to activate the spring profiles. In the below example, we will use a mock 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.

@MockEndpoints creates mock endpoints in your Camel route.

Java
 




x
41


 
1
package com.demo.camelspringboot.route;
2

          
3
import org.apache.camel.EndpointInject;
4
import org.apache.camel.ProducerTemplate;
5
import org.apache.camel.component.mock.MockEndpoint;
6
import org.apache.camel.test.spring.MockEndpoints;
7
import org.junit.Test;
8
import org.junit.runner.RunWith;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.boot.test.context.SpringBootTest;
11
import org.springframework.core.env.Environment;
12
import org.springframework.test.context.ActiveProfiles;
13
import org.springframework.test.context.junit4.SpringRunner;
14

          
15
@ActiveProfiles("mock")
16
@RunWith(SpringRunner.class)
17
@MockEndpoints("output")
18
@SpringBootTest
19
public class RouteMockTest{
20

          
21
    @Autowired
22
    ProducerTemplate producerTemplate;
23

          
24
    @Autowired
25
    Environment environment;
26

          
27
    @EndpointInject("mock:output")
28
    MockEndpoint mock;
29

          
30
    @Test
31
    public void testFileMovementMock() throws InterruptedException {
32
        String targetEnvironment=environment.getProperty("targetEnvironment");
33
        String fileContent="This is a file for "+targetEnvironment;
34
        mock.expectedMessageCount(1);
35
        mock.expectedBodiesReceived(fileContent);
36
        producerTemplate.sendBodyAndHeader(environment.getProperty("routeStart"), fileContent,
37
                "environment",targetEnvironment);
38
        mock.assertIsSatisfied();
39
    }
40
}
41

          



In the RouteMockTest class, the environment header is passed with value mock, and based on this value, the CamelRoute class does a choice between pollEnrich and log.

Java
 




xxxxxxxxxx
1
20


 
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
                .choice()
13
                    .when(header("environment").isNotEqualTo("mock"))
14
                        .pollEnrich("{{routeFrom}}")
15
                    .otherwise()
16
                        .log("Mock flow called with body ${body}")
17
                .to("{{routeTo}}");
18
    }
19
}
20

          


application-mock.yml

YAML
 




xxxxxxxxxx
1


 
1
server:
2
  port: 8081
3

          
4
routeStart: direct:input
5
routeFrom: file:dev/input?delete=true
6
routeTo: mock:output
7
targetEnvironment: mock



Run the RouteMockTest class and observe that the test case is passed.

Test case passed

Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

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