Camel Route: Mock Testing
This article covers the mock testing of a SpringBoot Camel Route. Camel provides two components to perform mock testing.
Join the DZone community and get the full member experience.
Join For FreeThis 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.
package com.demo.camelspringboot.route;
import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.MockEndpoints;
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;
"mock") (
SpringRunner.class) (
"output") (
public class RouteMockTest{
ProducerTemplate producerTemplate;
Environment environment;
"mock:output") (
MockEndpoint mock;
public void testFileMovementMock() throws InterruptedException {
String targetEnvironment=environment.getProperty("targetEnvironment");
String fileContent="This is a file for "+targetEnvironment;
mock.expectedMessageCount(1);
mock.expectedBodiesReceived(fileContent);
producerTemplate.sendBodyAndHeader(environment.getProperty("routeStart"), fileContent,
"environment",targetEnvironment);
mock.assertIsSatisfied();
}
}
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
.
xxxxxxxxxx
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")
.choice()
.when(header("environment").isNotEqualTo("mock"))
.pollEnrich("{{routeFrom}}")
.otherwise()
.log("Mock flow called with body ${body}")
.to("{{routeTo}}");
}
}
application-mock.yml
xxxxxxxxxx
server
port8081
routeStart direct input
routeFrom file dev/input?delete=true
routeTo mock output
targetEnvironment mock
Run the RouteMockTest
class and observe that the test case is passed.
Opinions expressed by DZone contributors are their own.
Comments