Apache Camel JUnit Testing using Simple example | JavaInUse







Apache Camel Unit Testing

In this tutorial we do unit testing for Apache Camel Spring Application using CamelSpringTestSupport.
In a previous post we had integrated Apache Camel and AcriveMQ. We will be using a similar example here.

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

We will create Eclipse maven project as follows-

Apache Camel Unit Testing Example
Our pom file will be as follows. It will have the camel test dependency
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javainuse</groupId>
  <artifactId>apache-camel-junit</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.13.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-test-spring</artifactId>
			<version>2.13.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version>2.13.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jms</artifactId>
			<version>2.14.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-camel</artifactId>
			<version>5.7.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
			<version>5.10.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-client</artifactId>
			<version>5.10.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.10.1</version>
		</dependency>
	</dependencies>
</project>

Next define the class to configure route-
package com.javainuse;

import org.apache.camel.builder.RouteBuilder;

public class SimpleRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("{{route.start}}").split().tokenize("\n").to("{{route.end}}");
    }

}
  

Next define the xml configuration which defines the jms and camel context in the main/resources
  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/cxf
       http://camel.apache.org/schema/cxf/camel-cxf.xsd 
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd
       http://www.springframework.org/schema/jdbc 
       http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd ">

<bean class="org.apache.activemq.camel.component.ActiveMQComponent"
		id="jms">
		<property name="connectionFactory" ref="singleCF" />
		<property name="useSingleConnection" value="true" />
		<property name="usePooledConnection" value="false" />
		<property name="preserveMessageQos" value="true" />
	</bean>

	<bean class="org.springframework.jms.connection.SingleConnectionFactory"
		id="singleCF">
		<property name="targetConnectionFactory" ref="AMQCF" />
	</bean>

	<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="AMQCF">
		<property name="userName" value="admin" />
		<property name="password" value="admin" />
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>
	
	<bean id="simpleRouter" class="com.javainuse.SimpleRouteBuilder" />

	<camelContext id="simpleRouterContext" xmlns="http://camel.apache.org/schema/spring">

		<propertyPlaceholder id="placeholder" location="classpath:camel.properties" />
		<routeBuilder ref="simpleRouter" />

	</camelContext>

</beans>
  
Define the camel.properties file in main/resources as follows-
route.start=file:C:/inbox?noop=true
route.end=jms:queue:javainuse
 

Define the main class to load the camel-context and start the route-
 package com.javainuse;

import org.apache.camel.CamelContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
    	 ApplicationContext springCtx = new ClassPathXmlApplicationContext("camel-context.xml");

          CamelContext ctx = springCtx.getBean("simpleRouterContext", CamelContext.class);

          try {
            ctx.start();
            Thread.sleep(5 * 60 * 1000);
            ctx.stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}
  

Now start the ActiveMQ.

and goto http://localhost:8161/admin/queues.jsp, we will see all no active queues on this page.

Next run the MainApp class as a java application and refresh the above activeMQ page. We will see a new queue named javainuse is created and it has messages in it.
Next we define the Test class as follows-
We load the camel-context.xml defined in the test/resources folder
Define the mock endpoint and send the message. Also we verify the message count for the test to pass.
  import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SimpleRouteBuilderTest 
    extends CamelSpringTestSupport {

	@Override
	protected AbstractApplicationContext createApplicationContext() {
		return new ClassPathXmlApplicationContext("camel-context.xml");
	}
	
	@Test
	public void testPayloadIsReached() 
	   throws InterruptedException {
	 MockEndpoint mockOut = getMockEndpoint("mock:out");
	 mockOut.setExpectedMessageCount(1);
	 template.sendBody("direct:in", "this is test");

	 assertMockEndpointsSatisfied();
	}
}
Next define the camel-context in the test/resources folder as follows-
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/cxf
       http://camel.apache.org/schema/cxf/camel-cxf.xsd 
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd
       http://www.springframework.org/schema/jdbc 
       http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd ">

<bean class="org.apache.activemq.camel.component.ActiveMQComponent"
		id="jms">
		<property name="connectionFactory" ref="singleCF" />
		<property name="useSingleConnection" value="true" />
		<property name="usePooledConnection" value="false" />
		<property name="preserveMessageQos" value="true" />
	</bean>

	<bean class="org.springframework.jms.connection.SingleConnectionFactory"
		id="singleCF">
		<property name="targetConnectionFactory" ref="AMQCF" />
	</bean>

	<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="AMQCF">
		<property name="userName" value="admin" />
		<property name="password" value="admin" />
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>
	
	<bean id="simpleRouter" class="com.javainuse.SimpleRouteBuilder" />

	<camelContext id="simpleRouterContext" xmlns="http://camel.apache.org/schema/spring">

		<propertyPlaceholder id="placeholder" location="classpath:camel.properties" />
		<routeBuilder ref="simpleRouter" />

	</camelContext>

</beans> 
Define the camel.properties in the test/resources for pocking the camel endpointd as follows-
route.start=direct:in
route.end=mock:out

Run the SimpleRouteBuilderTest as a JUnit as follows-

Apache Camel Unit Testing Tutorial

Download Source Code

Download it - Apache Camel JUnit Testing

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions