7 Replies Latest reply on Nov 16, 2009 11:13 AM by davsclaus

    JUnit test and bean endpoint in route

    mfatafta

      I m trying to write a JUnit test case for a camel route. The route that I am trying to test uses a bean URI in one of its endpoints.

       

      When I run the JUnit test, I get the following exception:

       

      org.apache.camel.component.bean.NoBeanAvailableException: No bean available for endpoint: myBean

       

      I tried to use @ContextConfiguration, to load an xml file with the bean definition, but no lock. Any idea on how to creat the bean and make it available to the camelConext?

       

      Thanks in advance.

        • 1. Re: JUnit test and bean endpoint in route
          davsclaus

          Just define it as a spring bean

           

           

          • 2. Re: JUnit test and bean endpoint in route
            mfatafta

            It is.

             

            My route is defined in a RouteBuilder java file, the route looks something like this:

             

            from ( sourceUri )

                 .inOut ( "validator:xsd/validateRequest.xsd" )

                 .inOut( "bean:myRequestBean" )

                 .inOut ( internalSourceDirectEndPoint )

            ;

             

            myRequestBean is defined as a spring bean in an xml file like this:

             

             

            I am trying to create JUnit test case for my route. How do I load the bean into my JUnit test camelContext?

             

            Does anyone have an sample code that I can follow?

            • 3. Re: JUnit test and bean endpoint in route
              njiang

              You can try to let spring load the route rule just like this

                 <camelContext id="camel5" xmlns="http://camel.apache.org/schema/spring">
                  <routeBuilder ref="myBuilder"></routeBuilder>    
                </camelContext>
                
                <bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"></bean>
              

               

              • 4. Re: JUnit test and bean endpoint in route
                mfatafta

                I don't think you understand my question.

                 

                In my JUnit test, I am trying to load a camel route. That camel route references a java bean in one of its endpoint.

                 

                When I run my JUnit test I get the following exception:

                org.apache.camel.component.bean.NoBeanAvailableException: No bean available for endpoint: myRequestBean

                • 5. Re: JUnit test and bean endpoint in route
                  davsclaus

                  If its a pure junit test, eg extending TestCase or the likes you need to bind your bean to the Camel Registry so it knows how to find it.

                   

                  for example using JNDI

                  Or creating your own implementation of Camel Registry.

                   

                  In Camel 2.1 there is a SimpleRegistry out of the box that is based on a Map so its easy to bind using put.

                   

                  You can do it something like this:

                   

                  JndiContext ctx = new org.apache.camel.util.jndi.JndiContext();
                  cxt.bind("foo", new MyFooBean();
                  
                  CamelContext context = new DefaultCameContext(ctx);
                  

                   

                  • 6. Re: JUnit test and bean endpoint in route
                    mfatafta

                    Will the same works for Camel 1.6.1?

                     

                    Thanks

                    • 7. Re: JUnit test and bean endpoint in route
                      davsclaus

                      Yeah you can use the Jndi code I showed.