Camel Junit Test Using Spring DSL

Camel Junit Test Example Using Spring DSL with simple steps.


In this tutorial we create one route in Spring file and test it using camel inbuilt Test support.To test spring based route we extends SpringCamelTestSupport class.

  • In eclipse go to MyFirstCamelProject that we created in earlier page and right click on src/test/java to create package.New -> package



  • Give any name for example com.preparationforinterview.spring.filetransfer and click on Finish



  • Right click on package and create a java class for example FileTransferinSpringTest.



  • Right click on src/main/resources and and create one .xml file for example MySpring.xml.



  • Add following dependency in pom.xml file.you can use any latest version.

  •  	
    <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>MyFirstCamelProject</groupId>
    	<artifactId>MyFirstCamelProject</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</artifactId>
    			<version>2.5.0</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-spring</artifactId>
    			<version>2.13.0</version>
    		</dependency>
    	</dependencies>
    </project>
    
  • Run mvn:install from command prompt or if you are using Eclipse right click on pom.xml and run mvn install.It will download camel jars

  • Code for Junit Test

  • At the end we will have following structure


  • Copy below code in FileTransferinSpringTest.

  •  	
    package com.preparationforinterview.spring.filetransfer;
    
    import java.io.File;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.test.junit4.CamelSpringTestSupport;
    import org.junit.Test;
    import org.springframework.context.support.AbstractXmlApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class FileTransferinSpringTest extends CamelSpringTestSupport {
    	@Override
    	protected AbstractXmlApplicationContext createApplicationContext() {
    		return new ClassPathXmlApplicationContext("MySpring.xml");
    	}
    
    	public void setUp() throws Exception {
    		deleteDirectory("input");
    		deleteDirectory("output");
    		super.setUp();
    	}
    
    	@Test
    	public void testFileCopyInSpring() throws Exception {
    		template.sendBodyAndHeader("file:input", "Hello World",
    				Exchange.FILE_NAME, "hello.txt");
    		Thread.sleep(1000);
    		File target = new File("output/hello.txt");
    		assertTrue("File not moved", target.exists());
    		String content = context.getTypeConverter().convertTo(String.class,
    				target);
    		assertEquals("Hello World", content);
    	}
    }
    
    

    Explain

    To test spring based route we need to extend CamelSpringTestSupport.CamelSpringTestSupport class is wrapped on Junit.When we extend CamelSpringTestSupport then we need to override one method createApplicationContext.In that method create an object that specify how are we reading file Here we have used ClassPathXmlApplicationContext.To test router we created method annotated with @Test. In that method we are sending Hello World text hello.txt file.To give time to copy we are waiting 1 second.After that as route in MySpring.xml should copy file to output folder ,so we pick that file and check content.It should be same as input file.template and context we get from CamelSpringTestSupport.

  • Write lines in MySpring.xml.

  •  	
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans.xsd          
    		http://camel.apache.org/schema/spring 
    		http://camel.apache.org/schema/spring/camel-spring.xsd">
    
    	<camelContext xmlns="http://camel.apache.org/schema/spring">
    		<route>
    			<from uri="file:input?noop=true" />
    			<to uri="file:output" />
    		</route>
    	</camelContext>
    
    </beans>
    

    Explain

    Last entry in xml file <camelContext xmlns="http://camel.apache.org/schema/spring"/> creates SpringCamelContext which provides CamelContext.In CamelContext we have add route so when we start spring application that routes also start

    To check the output right click on FileTransferinSpringTest.java and run as Junit. It will pass test case.
    To run the program from command prompt,open command prompt and go to project location and run below command.
    mvn compile exec:java -Dexec.mainClass=com.preparationforinterview.spring.filetransfer.FileTransferinSpringTest

Download source code

Camel Junit Test Using Spring DSL



Visit Others Links

Camel Introduction
Camel Setup
File Copy Example Using Camel
File Copy Example In Camel Using Spring
JMS Message Transfer Example Using Camel
JMS Message Transfer Example In Camel Using Spring
Processor In Camel
Object to JSON Transfer Example Using Spring In Camel
Camel With Bean Injection Example Using Spring DSL
Camel With Bean Injection Registry Example Using Java DSL
Camel Bean Parameter Binding Example Using Java DSL
Camel Error Handling With Redelivery Example Using Java DSL
Camel Exception Handling With Example Using Java DSL
Camel JUnit Test With Example Using Java DSL
Camel JUnit Test With Example Using Spring DSL
Camel Unit Test With Mock Component Example
Camel With Stream Component Example
Camel In-Memory Messaging Example Using Direct
Camel With Timer And Quartz Example
Camel Aggregator EIP Example
Camel Splitter EIP Example
Camel Routing Slip EIP Example
Camel Dynamic Router EIP Example
Camel Load Balancer EIP Example
Top Camel Interview Question
© 2019 PreparationForInterview.com