camel Unit Test With Mock Component

Camel Junit Test Example Using Mock Component with simple steps.


In this tutorial we create one route and test it using camel inbuilt mock component mock component comes with camel-core.mock endpoint is implemented as org.apache.camel.component. mock.MockEndpoint class.When we test using mock component then we set the expectation and after executing test we verify our test against that expectation.

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



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



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



  • 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-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>
    	</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

  • Copy below code in FileCopyExample.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class FileCopyExample extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    		from("file:input1?noop=true").to("mock:endpoint");
    
    		from("file:input2?noop=true").to("mock:endpoint");
    	}
    
    }
    
    

    Explain

    To create a route in camel we need to extend RouteBuilder class and override configure method.In configure method we define our route.In above example we can read it as from input folder take file and send it to mock endpoint.Here file: is called endpoint.We have set up two route so that we can verify both message.

    Code for Junit Test With Mock Endpoint

  • Now to test above route we will create one test class for example FileCopyExampleTest


  • At the end we will have following structure


  • Copy below code in FileCopyExampleTest.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.component.mock.MockEndpoint;
    import org.apache.camel.test.junit4.CamelTestSupport;
    import org.junit.Test;
    
    public class FileCopyExampleTest extends CamelTestSupport {
    	@Override
    	protected RouteBuilder createRouteBuilder() throws Exception {
    		return new FileCopyExample();
    	}
    
    	public void setUp() throws Exception {
    		deleteDirectory("input1");
    		deleteDirectory("input2");
    		super.setUp();
    	}
    
    	@Test
    	public void testFileCopy() throws Exception {
    		MockEndpoint mockEndpoint = getMockEndpoint("mock:endpoint");
    		mockEndpoint.expectedMessageCount(2);
    		mockEndpoint.expectedBodiesReceivedInAnyOrder("Hello World",
    				"Hello World1");
    
    		template.sendBodyAndHeader("file:input1", "Hello World",
    				Exchange.FILE_NAME, "hello.txt");
    		template.sendBodyAndHeader("file:input2", "Hello World1",
    				Exchange.FILE_NAME, "hello.txt");
    
    		mockEndpoint.assertIsSatisfied();
    
    	}
    }
    
    

    Explain

    To get camel test kit advantage we need to extend CamelTestSupport.CamelTestSupport class is wrapped on Junit.When we extend CamelTestSupport then we need to override one method createRouteBuilder.In that method create an object of route class.In our case we have done FileCopyExample.To test router we created method annotated with @Test. In that method we are getting mock endpoint and we have set expectation for example Expected message count will be 2 and their content.After expectation has been set,we are sending message to input.To Test execution with our result we fire assertion. template, we get from CamelTestSupport.

    To check the output right click on FileCopyExampleTest.java and run it.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.FileCopyExampleTest

Download source code

Camel Junit Test With Mock Component



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