How to unit test this route builder in camel?

12,748

Solution 1

As you use @Mock, MockitoAnnotations.initMocks has to be invoked. Additionally, when has also be called before passing the reference to TestRoute:

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    // Initialize serviceConfiguration
    MockitoAnnotations.initMocks(this); 
    when(serviceConfiguration.getName()).thenReturn("file://target/inbox");
    return new TestRoute(serviceConfiguration);
}

Alternatively to @Mock and MockitoAnnotations.initMocks, just use:

serviceConfiguration = org.mockito.Mockito.mock(ServiceConfiguration.class);

As when is invoked in createRouteBuilder, serviceConfiguration.getName() always returns the same result for all test methods in the JUnit test class. This could be a problem, if different test methods need different URIs.

Alternatively, you may use adviceWith instead as described here.

Solution 2

Camel has two ways to do the route tests, CamelSpringTestSupport and CamelTestSupport.

CamelSpringTestSupport

You can set the route with some camel-whatever-test.xml to setup the route in Spring configuration xml.

CamelTestSupport

You can set the route with RouteBuilder createRoute. And configure the endpoint with mock components.

Here is some reference link, do not forget to add the dependency jars: http://camel.apache.org/testing.html

Share:
12,748
dejavu
Author by

dejavu

Updated on June 04, 2022

Comments

  • dejavu
    dejavu almost 2 years

    I have the following RouteBuilder Class and I am using dependency injection here. How can I test this route builder? I am using JUnit and camel test classes.

    public class TestRoute extends RouteBuilder {
    
        private ServiceConfiguration serviceConfiguration;
    
        public TestRoute(ServiceConfiguration serviceConfiguration) {
            this.serviceConfiguration = serviceConfiguration;
        }
    
        @Override
        public void configure() throws Exception {
            String incomingURI = serviceConfiguration.getQueueConfiguration().getURI();
            String outgoingURI = serviceConfiguration.getHTTPConfiguration().getURI();
            from(incomingURI).
            setHeader(Exchange.HTTP_METHOD, constant("PUT")).setHeader(Exchange.CONTENT_TYPE, constant("application/json")).
            to(outgoingURI);
        }
    }
    

    My thoughts:

    Creating a testConfiguration extending ServiceConfiguration and pass that. But for that I need to set all the configuration and all because the serviceConfiguration contains many other classes. Can I use Mockito here? What is the correct and easy way to test this?

    EDIT: I was using the existing route, so that I don't have to write it again. Looks like that is not the correct way of testing in camel. See my test class. Of course, its not working.

    public class RabbitMQRouteTest extends CamelTestSupport {
    
        @Mock
        ServiceConfiguration serviceConfiguration;
    
        @Override
        protected RouteBuilder createRouteBuilder() throws Exception {
            System.out.println("testing");
            return new TestRoute(serviceConfiguration);
        }
        @Test
        public void testConfigure() throws Exception {
            System.out.println("test");
    
            when(serviceConfiguration.getName()).thenReturn("file://target/inbox");
    
            template.sendBodyAndHeader("file://target/inbox", "Hello World",Exchange.FILE_NAME, "hello.txt");
            Thread.sleep(1000);
            File target = new File("target/outbox/hello.txt");
            assertTrue("File not moved", target.exists());
    
        }
    }
    
  • dejavu
    dejavu almost 10 years
    Ummm. You didn't answer the question. Its about testing the routebuilder when you use dependecy injection.
  • chenrui
    chenrui almost 10 years
    @dejavu, 1. register the routeBuilder in the camel-test.xml, 2. register the beans, 3. CamelSpringTestSupport to create the testcase.
  • dejavu
    dejavu almost 10 years
    I am unable to use mockito because as soon as I create an instance of TestRoute, the configure method is automatically called. So there is no option to set the mock objects and all. Also i am using dropwizard, so there is just one configuration file. Any other way to test it or if you can tell how to use mockito here
  • Christian Schneider
    Christian Schneider almost 10 years
    Not sure what you mean. How do you create the TestRoute and why should configure be called automatically. If you use the CamelTestSupport then you can create the TestRoute inside the createRouteBuilder method. There you can then call your constructor or setters to set necessary configuration.
  • dejavu
    dejavu almost 10 years
    See the EDIT in the question. I have written a temporary test class which I was using. Please suggest. For testing in camel, is it necessary to write the path again in routebuilder? Or we can do the testing by calling the implemented routebuilder. But as soon as I create an instance of that, configure will be called automatically.
  • Christian Schneider
    Christian Schneider almost 10 years
    Your test looks almost fine. Just create the ServiceConfiguration by hand instead of using @Mock. configure is not called on instance creation of the route. It is called by the camel test support when the camel context comes up.
  • dejavu
    dejavu almost 10 years
    Okay. Then what should I mock then? I didn't get where to use mockito then. Little bit new to the framework.
  • Christian Schneider
    Christian Schneider almost 10 years
    In the best case you do not need a mock framework at all. First try to instatiate the SeviceConfiguration with new and set the relevant values you need in your test. Only if this is not possible or too complicated use a mock framework for this. In this case create the ServiceConfiguration as a mock and make sure it returns the correct values.