You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by fordm <fo...@gmail.com> on 2013/02/07 10:40:51 UTC

Mocking an endpoint within a try-catch block

Hi,

Using Camel 2.10, I'm writing a test for the following camel route,
contained in a blueprint:

<route id="myRoute">
  <from uri="direct:sendEmail"/>
  <doTry>
    <log message="The message contains ${body}"/>
    <to id="smtpEndpoint" uri="smtp://localhost:25" />
    <doCatch>
      <exception>java.lang.Exception</exception>
      <log message="Exception caught when processing email request" />
    </doCatch>
    <doFinally>
      <log message="Email Response processed ${body}" />
    </doFinally>
  </doTry>
</route>

If the smtpEndpoint is outside the doTry then I can happily mock the
endpoint using adviceWith (and weaveById).

However, if the smtpEndpoint is inside the doTry mocking the endpoint with
adviceWith fails (the test tries to connect to a SMTP server on
localhost:25).

Is there something I'm missing or is it just not possible to mock endpoints
within a doTry (which I find hard to believe).

Thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Mocking-an-endpoint-within-a-try-catch-block-tp5727081.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Mocking an endpoint within a try-catch block

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Feb 7, 2013 at 3:08 PM, fordm <fo...@gmail.com> wrote:
> Claus Ibsen-2 wrote
>> Have you tried with
>>
>>          weaveById(endpointId).replace().to(newUri);
>
> Removing the selectFirst I get the same result.
>
> However, if I use
>
>     weaveByToString(uriPattern).selectFirst().replace().to(newUri);
>
> then the end point seems to be mocked successfully. Which is fine for the
> particular test I'm writing, but it would be good to know why I'm not able
> to mock endpoints by id.
>

Yeah the idea is that it does not matter how you match the nodes to mock.
Fell free to log a JIRA ticket, as this smells like a bug
http://camel.apache.org/support


>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Mocking-an-endpoint-within-a-try-catch-block-tp5727081p5727127.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Mocking an endpoint within a try-catch block

Posted by fordm <fo...@gmail.com>.
Claus Ibsen-2 wrote
> Have you tried with
> 
>          weaveById(endpointId).replace().to(newUri);

Removing the selectFirst I get the same result.

However, if I use

    weaveByToString(uriPattern).selectFirst().replace().to(newUri);

then the end point seems to be mocked successfully. Which is fine for the
particular test I'm writing, but it would be good to know why I'm not able
to mock endpoints by id.



--
View this message in context: http://camel.465427.n5.nabble.com/Mocking-an-endpoint-within-a-try-catch-block-tp5727081p5727127.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Mocking an endpoint within a try-catch block

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Feb 7, 2013 at 11:06 AM, fordm <fo...@gmail.com> wrote:
> I simply use
>
> context.getRouteDefinition(routeId).adviceWith(context,
>   new AdviceWithRouteBuilder() {
>       @Override
>       public void configure() {
>         weaveById(endpointId).selectFirst().replace().to(newUri);
>       }
>   });
>

Have you tried with

         weaveById(endpointId).replace().to(newUri);



> Debugging the test I can see that the the endpoint IS being mocked:
>
> AdviceWithTasks                INFO  AdviceWith (smtpEndpoint) :
> [To[smtp://localhost:25]] --> replace [pipeline -> [[To[mock:send]]]]
>
> But later in the debug output I find that the mock is not being used:
>
> DefaultComponent               DEBUG Creating endpoint
> uri=[smtp://localhost:25], path=[localhost:25], parameters=[{}]
> BlueprintCamelContext          DEBUG smtp://localhost:25 converted to
> endpoint: Endpoint[smtp://localhost:25] by component:
> org.apache.camel.component.mail.MailComponent@56e2aeb0
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Mocking-an-endpoint-within-a-try-catch-block-tp5727081p5727089.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Mocking an endpoint within a try-catch block

Posted by fordm <fo...@gmail.com>.
I simply use

context.getRouteDefinition(routeId).adviceWith(context,
  new AdviceWithRouteBuilder() {
      @Override
      public void configure() {
        weaveById(endpointId).selectFirst().replace().to(newUri);
      }
  });

Debugging the test I can see that the the endpoint IS being mocked:

AdviceWithTasks                INFO  AdviceWith (smtpEndpoint) :
[To[smtp://localhost:25]] --> replace [pipeline -> [[To[mock:send]]]]

But later in the debug output I find that the mock is not being used:

DefaultComponent               DEBUG Creating endpoint
uri=[smtp://localhost:25], path=[localhost:25], parameters=[{}]
BlueprintCamelContext          DEBUG smtp://localhost:25 converted to
endpoint: Endpoint[smtp://localhost:25] by component:
org.apache.camel.component.mail.MailComponent@56e2aeb0



--
View this message in context: http://camel.465427.n5.nabble.com/Mocking-an-endpoint-within-a-try-catch-block-tp5727081p5727089.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Mocking an endpoint within a try-catch block

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Feb 7, 2013 at 10:40 AM, fordm <fo...@gmail.com> wrote:
> Hi,
>
> Using Camel 2.10, I'm writing a test for the following camel route,
> contained in a blueprint:
>
> <route id="myRoute">
>   <from uri="direct:sendEmail"/>
>   <doTry>
>     <log message="The message contains ${body}"/>
>     <to id="smtpEndpoint" uri="smtp://localhost:25" />
>     <doCatch>
>       <exception>java.lang.Exception</exception>
>       <log message="Exception caught when processing email request" />
>     </doCatch>
>     <doFinally>
>       <log message="Email Response processed ${body}" />
>     </doFinally>
>   </doTry>
> </route>
>
> If the smtpEndpoint is outside the doTry then I can happily mock the
> endpoint using adviceWith (and weaveById).
>
> However, if the smtpEndpoint is inside the doTry mocking the endpoint with
> adviceWith fails (the test tries to connect to a SMTP server on
> localhost:25).
>
> Is there something I'm missing or is it just not possible to mock endpoints
> within a doTry (which I find hard to believe).
>

Can you share the advice with code you use?


> Thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Mocking-an-endpoint-within-a-try-catch-block-tp5727081.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen