Writing and running a Junit test - 8.0

Data Service and Routing Examples

Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
Talend Studio
Design and Development > Designing Routes
Design and Development > Designing Services
Last publication date


  1. Right-click the Maven project route-unit-test in the Package Explorer and select New > JUnit Test Case in the context menu.
    New JUnit Test Case wizard.
  2. The New JUnit Test Case wizard opens. Select New JUnit 3 test and enter RouteTestSample as the name of your test class. Click Finish.
  3. The test class opens in the design workspace. Write the test as follows.
    package org.talend.test;
    import java.util.Map;
    import org.apache.camel.CamelContext;
    import org.apache.camel.EndpointInject;
    import org.apache.camel.Produce;
    import org.apache.camel.ProducerTemplate;
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.component.mock.MockEndpoint;
    import org.apache.camel.test.junit4.CamelTestSupport;
    import org.junit.Test;
    import esbdemos.simpleroute_0_1.SimpleRoute;
    public class RouteTestSample extends CamelTestSupport {
    	@EndpointInject(uri = "mock:result")
    	protected MockEndpoint resultEndpoint;
    	@Produce(uri = "direct:start")
    	protected ProducerTemplate template;
    	private RouteBuilder builder;
    	public void testSendMatchingMessage() throws Exception {
    		String expectedBody = "bar";
    	public void testSendNotMatchingMessage() throws Exception {
    	protected CamelContext createCamelContext() throws Exception {
    		SimpleRoute route = new SimpleRoute();
    		// Setup the test aliases
    		Map<String, String> uriProperties = route.getUriMap();
    		uriProperties.put("start", "direct:start");
    		uriProperties.put("end", "mock:result");
    		// Build the route
    		builder = route.Route(false);
    		return builder.getContext();
    	protected RouteBuilder createRouteBuilder() {
    		return builder;

    As is shown above, we are going to use the Camel Test framework to test the Route, which requires to use special mockendpoints to extract results during the unit testing. But the Route we have created does not use such mock endpoints. It uses the usual file and log components. As a solution for that, we will replace the file and log components with the direct:start and mock:result components. In lines 49 to 52, you can see a Map<String,String> which maps the component name to it's URI, and replaces values for start and end. By doing so we are able to mock the components to test the Route in isolation.

  4. Click the Run Job button in the toolbar to run the test.
    JUnit tab.

    The test is successful.