Skip to main content Skip to complementary content

Writing and running a Junit test


  1. Right-click the Maven project route-unit-test in the Package Explorer and select New>JUnit Test Case in the context menu.
  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 button in the toolbar to run the test.

    The test is successful.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!