4 Replies Latest reply on Apr 16, 2013 1:17 PM by timgstewart

    how to mock blueprint beans?

    jasonnh

      Hi,

       

      I am using CamelBlueprintTestSupport to test camel routes defined in blueprint xml files. 

       

      Everything works OK if I stick to mocking endpoints but I need to mock a bean defined in the blueprint xml file.

       

      Does anybody have any ideas how to do this?

       

      Thanks.

       

      Edited by: jasonnh on Aug 6, 2012 10:59 AM

        • 1. Re: how to mock blueprint beans?
          jasonnh

          I have solved this by changing the bean to a Processor and using adviceWith:

           

          http://camel.apache.org/advicewith.html

          • 2. Re: how to mock blueprint beans?
            shenzy_shency.revindran

            hi Jason,

             

            how do I convert a bean definition to pocessor in blueprint?

             

            I have the  below and want to be able to mock the classA for my test.

             

             

             

            Thanks

             

            Edited by: shenzy on Apr 15, 2013 11:01 AM

            • 3. Re: how to mock blueprint beans?
              shenzy_shency.revindran

              Noticed that the bean should implement the org.apache.camel.Processor for this to work.

              But I don't want to make my spring bean a processor just for the unit test.

               

              Is there any other way of mocking beans inside blueprint? I have tried the spring extension springockito ( <mockito:mock/> ) - blueprint is not liking it.

              • 4. Re: how to mock blueprint beans?
                timgstewart

                #1.

                 

                You might be able to use a special blueprint.xml file for testing.

                 

                In your class that extends CamelBlueprintTestSupport, you can do:

                 

                    @Override

                    protected String getBlueprintDescriptor() {

                        return "OSGI-INF/blueprint/test-blueprint.xml,OSGI-INF/blueprint/blueprint.xml";

                    }

                 

                So that it reads both files in order.  I'm not sure if you want the test-blueprint.xml first or last.  In the end you want the mock bean to be the one that is in the registry so camel picks it up as the processor rather than the actual.

                 

                In this case you'd have to write a test processor and mock the methods yourself, or at the very least as a wrapper around some mock object that you inject before you run the routes.

                 

                You might also be able to programatically register the mock processor using the bundle context. 

                 

                I have done this with services that my routes are expecting, but not with processors (using the registerService method). 

                 

                #2.

                 

                You might be able to change the ProcessorFactory in the camel context, and return a different processor when camel requests it.

                 

                http://camel.apache.org/processorfactory.html

                 

                I haven't tried this.

                 

                #3.

                 

                I've done this:

                 

                        route.adviceWith(context, new RouteBuilder() {

                            @Override

                            public void configure() throws Exception {

                                interceptSendToEndpoint("myProcessor").

                                        skipSendToOriginalEndpoint().

                                        process(new Processor() {

                                   @Override

                                   public void process(Exchange exchng) throws Exception {

                                    // do something different here.

                                   }

                               }).

                                        to("mock:myMockEndpoint");

                 

                Intercepting camel before it sends to the processor, tell it not to send to the original endpoint, do something else in a mock fashion, and send it to a mock endpoint to count exchanges.  You could also send it onto a live endpoint if you wanted.