You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by catequil <br...@yahoo.com> on 2016/10/11 20:54:39 UTC

Camel Spring Unit Testing with Mocks

Background

I'm a newbie to Apache Camel. (3 months)
I have read the testing section to Camel in Action MEAP version.
I have read http://camel.apache.org/testing.html and
http://camel.apache.org/spring-testing.html
I have searched this forum, google and stackoverflow for answers.
I've tried using the adviceWith, but that is confusing too.
Current versions are Camel 2.16.2 and Spring 4.0.6 and Junit 4.12
Dependencies loaded: {camel-core, camel-spring, camel-cxf, camel-jackson,
camel-test, camel-test-spring40, camel-kafka, spring-test}

Criteria

DRY (No identical code in testing)
Cannot modify production code.
Must use Mocks as integration tests would take too long to run.
Must use CamelSpringTestSupport (The only configuration I can get to work in
my environment that supports spring and camel)

Question

The part I am struggling with the most, for a week now, is the Mocks.  They
do not make sense to me on how they work in relation to the production
version of the route.
Can some one please help me to create a unit test that works having all this
criteria and help me understand the Mocking for Endpoints? 

 





--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Spring Unit Testing with Mocks

Posted by Tomohisa Igarashi <tm...@gmail.com>.
Actually interceptSendToEndpoint applies over route collections, but not on a specific route
https://github.com/apache/camel/blob/master/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java#L259

On 10/15/2016 03:36 PM, Tomohisa Igarashi wrote:
> I'm not sure if it's a bug or misuse, but expectedBodyReceived().body().isEqualTo("expected") doesn't work as expected. expectedBodiesReceived("expected") or expectedBodyReceived().constant("expected") works fine.
> https://github.com/igarashitm/issues/blob/master/camel/misc/src/test/java/ExpectedBodyReceivedTest.java
> only testBodyIsEqualTo() fails.
>
> No idea yet for your second issue, but it sounds like the all endpoints are intercepted regardless of the RouteDefinition you invoked adviceWith().
>
> Thanks,
> Tomo
>
> On 10/15/2016 02:06 AM, catequil wrote:
>> Ok, so it appears that I understand and have a working copy.  There are
>> however a few remaining questions.  If you feel they are no longer relevant
>> in this thread let me know and I will create a new thread.
>>
>> In the code below I expected it to grab the "myRouteMiddle" route and find
>> the endpoint(direct:end) intercept it and mock the direct:end as
>> mock:direct:end.
>>
>> In the assertions I expected it to to have a body of "peace world!", but the
>> test passed with a body of "hello world!".
>>
>> Am I misunderstanding how the intercepts and/or getRouteDefinition work?
>>
>> On a second issue I intercepted "log:input" and the test failed saying there
>> were 3 messages.  I expected it to only have one based on the following
>> criteria:
>> 1. the "skipSendToOriginalEndpoint" should have stopped the last "log:input"
>> from being activated.
>> 2. the getRouteDefinition was set to get "myMiddleRoute" which in my mind
>> should have gotten just the middle route and not the start route making the
>> total messages just 1.
>>
>> Do you know why this works this way?  Is this a bug?  Or am I miss
>> understanding how it works?
>>
>> Thanks
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635p5788814.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>

Re: Camel Spring Unit Testing with Mocks

Posted by Tomohisa Igarashi <tm...@gmail.com>.
I'm not sure if it's a bug or misuse, but expectedBodyReceived().body().isEqualTo("expected") doesn't work as expected. expectedBodiesReceived("expected") or expectedBodyReceived().constant("expected") works fine.
https://github.com/igarashitm/issues/blob/master/camel/misc/src/test/java/ExpectedBodyReceivedTest.java
only testBodyIsEqualTo() fails.

No idea yet for your second issue, but it sounds like the all endpoints are intercepted regardless of the RouteDefinition you invoked adviceWith().

Thanks,
Tomo

On 10/15/2016 02:06 AM, catequil wrote:
> Ok, so it appears that I understand and have a working copy.  There are
> however a few remaining questions.  If you feel they are no longer relevant
> in this thread let me know and I will create a new thread.
>
> In the code below I expected it to grab the "myRouteMiddle" route and find
> the endpoint(direct:end) intercept it and mock the direct:end as
> mock:direct:end.
>
> In the assertions I expected it to to have a body of "peace world!", but the
> test passed with a body of "hello world!".
>
> Am I misunderstanding how the intercepts and/or getRouteDefinition work?
>
> On a second issue I intercepted "log:input" and the test failed saying there
> were 3 messages.  I expected it to only have one based on the following
> criteria:
> 1. the "skipSendToOriginalEndpoint" should have stopped the last "log:input"
> from being activated.
> 2. the getRouteDefinition was set to get "myMiddleRoute" which in my mind
> should have gotten just the middle route and not the start route making the
> total messages just 1.
>
> Do you know why this works this way?  Is this a bug?  Or am I miss
> understanding how it works?
>
> Thanks
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635p5788814.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Camel Spring Unit Testing with Mocks

Posted by catequil <br...@yahoo.com>.
Ok, so it appears that I understand and have a working copy.  There are
however a few remaining questions.  If you feel they are no longer relevant
in this thread let me know and I will create a new thread.

In the code below I expected it to grab the "myRouteMiddle" route and find
the endpoint(direct:end) intercept it and mock the direct:end as
mock:direct:end.

In the assertions I expected it to to have a body of "peace world!", but the
test passed with a body of "hello world!".

Am I misunderstanding how the intercepts and/or getRouteDefinition work?

On a second issue I intercepted "log:input" and the test failed saying there
were 3 messages.  I expected it to only have one based on the following
criteria:  
1. the "skipSendToOriginalEndpoint" should have stopped the last "log:input"
from being activated.
2. the getRouteDefinition was set to get "myMiddleRoute" which in my mind
should have gotten just the middle route and not the start route making the
total messages just 1.

Do you know why this works this way?  Is this a bug?  Or am I miss
understanding how it works?

Thanks





--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635p5788814.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Spring Unit Testing with Mocks

Posted by Tomohisa Igarashi <tm...@gmail.com>.
> First, is this the recommended way to do this type of test?

I don't know the "official words", but it's used in official unit tests and also introduced in Camel in Action.

> Second, can the createRouteBuilder be the one in the actual prod route or do I have to copy my prod route into it?

You'd load your prod spring XML route via CamelSpringTestSupport, retrieve the RouteDefinition from context by its name and adviceWith() like this test case
https://github.com/apache/camel/blob/master/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/AdviceWithOnExceptionMultipleIssueTest.java#L44

Another option would be to use @MockEndpointsAndSkip
https://github.com/apache/camel/blob/master/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRunnerMockEndpointsAndSkipTest.java#L29

Thanks,
Tomo

On 10/12/2016 10:15 PM, Ryan Tracy wrote:
> Tomo,
> That looks like what I am looking for.  Two questions.  First, is this the recommended way to do this type of test?Second, can the createRouteBuilder be the one in the actual prod route or do I have to copy my prod route into it?
> Ryan
>
> Sent from Yahoo Mail on Android
>
>   On Wed, Oct 12, 2016 at 12:54 AM, Tomohisa Igarashi<tm...@gmail.com> wrote:   Hi,
>
> Is this what you're looking for as an example? interceptSendToEndpoint()&skipSendToOriginalEndpoint() via AdviceWithRouteBuilder intercepts sending to the actual endpoint defined in spring XML and forwarding it to the mock endpoint for testing purpose.
> https://github.com/apache/camel/blob/master/components/camel-http/src/test/java/org/apache/camel/component/http/HttpInterceptSendToEndpointTest.java#L35
>
> Thanks,
> Tomo
>
> On 10/12/2016 05:54 AM, catequil wrote:
>> Background
>>
>> I'm a newbie to Apache Camel. (3 months)
>> I have read the testing section to Camel in Action MEAP version.
>> I have read http://camel.apache.org/testing.html and
>> http://camel.apache.org/spring-testing.html
>> I have searched this forum, google and stackoverflow for answers.
>> I've tried using the adviceWith, but that is confusing too.
>> Current versions are Camel 2.16.2 and Spring 4.0.6 and Junit 4.12
>> Dependencies loaded: {camel-core, camel-spring, camel-cxf, camel-jackson,
>> camel-test, camel-test-spring40, camel-kafka, spring-test}
>>
>> Criteria
>>
>> DRY (No identical code in testing)
>> Cannot modify production code.
>> Must use Mocks as integration tests would take too long to run.
>> Must use CamelSpringTestSupport (The only configuration I can get to work in
>> my environment that supports spring and camel)
>>
>> Question
>>
>> The part I am struggling with the most, for a week now, is the Mocks.  They
>> do not make sense to me on how they work in relation to the production
>> version of the route.
>> Can some one please help me to create a unit test that works having all this
>> criteria and help me understand the Mocking for Endpoints?
>>
>>
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>

Re: Camel Spring Unit Testing with Mocks

Posted by Ryan Tracy <br...@yahoo.com.INVALID>.
Tomo,
That looks like what I am looking for.  Two questions.  First, is this the recommended way to do this type of test?Second, can the createRouteBuilder be the one in the actual prod route or do I have to copy my prod route into it?
Ryan

Sent from Yahoo Mail on Android 
 
  On Wed, Oct 12, 2016 at 12:54 AM, Tomohisa Igarashi<tm...@gmail.com> wrote:   Hi,

Is this what you're looking for as an example? interceptSendToEndpoint()&skipSendToOriginalEndpoint() via AdviceWithRouteBuilder intercepts sending to the actual endpoint defined in spring XML and forwarding it to the mock endpoint for testing purpose.
https://github.com/apache/camel/blob/master/components/camel-http/src/test/java/org/apache/camel/component/http/HttpInterceptSendToEndpointTest.java#L35

Thanks,
Tomo

On 10/12/2016 05:54 AM, catequil wrote:
> Background
>
> I'm a newbie to Apache Camel. (3 months)
> I have read the testing section to Camel in Action MEAP version.
> I have read http://camel.apache.org/testing.html and
> http://camel.apache.org/spring-testing.html
> I have searched this forum, google and stackoverflow for answers.
> I've tried using the adviceWith, but that is confusing too.
> Current versions are Camel 2.16.2 and Spring 4.0.6 and Junit 4.12
> Dependencies loaded: {camel-core, camel-spring, camel-cxf, camel-jackson,
> camel-test, camel-test-spring40, camel-kafka, spring-test}
>
> Criteria
>
> DRY (No identical code in testing)
> Cannot modify production code.
> Must use Mocks as integration tests would take too long to run.
> Must use CamelSpringTestSupport (The only configuration I can get to work in
> my environment that supports spring and camel)
>
> Question
>
> The part I am struggling with the most, for a week now, is the Mocks.  They
> do not make sense to me on how they work in relation to the production
> version of the route.
> Can some one please help me to create a unit test that works having all this
> criteria and help me understand the Mocking for Endpoints?
>
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
  

Re: Camel Spring Unit Testing with Mocks

Posted by Minh Tran <da...@gmail.com>.
Hi

I had this same issue too when I started looking into unit testing in camel. The mocks in camel aren’t really like mocks that comes with libraries such as Mockito. Those mocks actually replace the interface in which it mocks. Whereas the mocks in camel act more like proxies to the endpoints. Since they are proxies, they can optionally choose to skip the endpoint for which it is proxying. 

So in order to do what you want in your criteria. 
1. Load up your camel routes you wish to test.
2. Tell camel which endpoint you want to mock, this varies depending on which test method you want to use. CamelSpringTestSupport uses inheritance whereas the Camel enhanced sprint test uses annotations. Both has some way of accepting a uri pattern to tell it which endpoints you want to mock.
3. At the same time you do step 2, you normally specify whether to optionally skip the endpoint it is mocking. In your case, you want it to skip.
4. Inject the MockEndpoint into your unit test, I think there are various ways I used @EndpointInject and then set expectations on them. Since you’re skipping the endpoints, you should mock the output as well especially if your original endpoint actually had output. Now execute your route using a ProducerTemplate and then finally verify that your MockEndpoints received what you expected.

You can find a lot more info in that link you got below http://camel.apache.org/spring-testing.html


> On 10/12/2016 05:54 AM, catequil wrote:
>> Background
>> 
>> I'm a newbie to Apache Camel. (3 months)
>> I have read the testing section to Camel in Action MEAP version.
>> I have read http://camel.apache.org/testing.html and
>> http://camel.apache.org/spring-testing.html
>> I have searched this forum, google and stackoverflow for answers.
>> I've tried using the adviceWith, but that is confusing too.
>> Current versions are Camel 2.16.2 and Spring 4.0.6 and Junit 4.12
>> Dependencies loaded: {camel-core, camel-spring, camel-cxf, camel-jackson,
>> camel-test, camel-test-spring40, camel-kafka, spring-test}
>> 
>> Criteria
>> 
>> DRY (No identical code in testing)
>> Cannot modify production code.
>> Must use Mocks as integration tests would take too long to run.
>> Must use CamelSpringTestSupport (The only configuration I can get to work in
>> my environment that supports spring and camel)
>> 
>> Question
>> 
>> The part I am struggling with the most, for a week now, is the Mocks.  They
>> do not make sense to me on how they work in relation to the production
>> version of the route.
>> Can some one please help me to create a unit test that works having all this
>> criteria and help me understand the Mocking for Endpoints?
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>> 


Re: Camel Spring Unit Testing with Mocks

Posted by Tomohisa Igarashi <tm...@gmail.com>.
Hi,

Is this what you're looking for as an example? interceptSendToEndpoint()&skipSendToOriginalEndpoint() via AdviceWithRouteBuilder intercepts sending to the actual endpoint defined in spring XML and forwarding it to the mock endpoint for testing purpose.
https://github.com/apache/camel/blob/master/components/camel-http/src/test/java/org/apache/camel/component/http/HttpInterceptSendToEndpointTest.java#L35

Thanks,
Tomo

On 10/12/2016 05:54 AM, catequil wrote:
> Background
>
> I'm a newbie to Apache Camel. (3 months)
> I have read the testing section to Camel in Action MEAP version.
> I have read http://camel.apache.org/testing.html and
> http://camel.apache.org/spring-testing.html
> I have searched this forum, google and stackoverflow for answers.
> I've tried using the adviceWith, but that is confusing too.
> Current versions are Camel 2.16.2 and Spring 4.0.6 and Junit 4.12
> Dependencies loaded: {camel-core, camel-spring, camel-cxf, camel-jackson,
> camel-test, camel-test-spring40, camel-kafka, spring-test}
>
> Criteria
>
> DRY (No identical code in testing)
> Cannot modify production code.
> Must use Mocks as integration tests would take too long to run.
> Must use CamelSpringTestSupport (The only configuration I can get to work in
> my environment that supports spring and camel)
>
> Question
>
> The part I am struggling with the most, for a week now, is the Mocks.  They
> do not make sense to me on how they work in relation to the production
> version of the route.
> Can some one please help me to create a unit test that works having all this
> criteria and help me understand the Mocking for Endpoints?
>
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-Unit-Testing-with-Mocks-tp5788635.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>