You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Minh Tran <mi...@winning.com.au> on 2015/11/16 02:40:00 UTC

spring boot test mocks

Hi

I’m trying to write unit tests in camel 2.16.0 and spring boot.

My route looks like

from(“direct:start”).to(“direct:end”)

My unit test looks like

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Config.class)
@MockEndpoints
public class MyUnitTest {

  @Produce(uri=“direct:start”)
  private ProducerTemplate producer;

  @EndpointInject(uri=“mock:direct:end”)
  private MockEndpoint end;

	@Test
	public void testMock() throws InterruptedException {
		end.expectedBodiesReceived("blah");
		producerTemplate.sendBody("blah");
		end.assertIsSatisfied();
	}

}

It looks like the direct:end bit is never mocked so the assertion fails. It’s like @MockEndpoints is completely ignored. 

Is this the correct way to mock existing components when using spring boot? Thanks.

Re: spring boot test mocks

Posted by Joakim Bjørnstad <jo...@gmail.com>.
Hello Minh,

Yeah it is a little bit awkward. At the moment I make microservice
style apps with Spring Boot and Camel and the time from prototype to
production goes a lot faster with all the free Spring Boot stuffs.

The way we test these Camel routes are two-fold:

Unit tests per SpringRouteBuilder implementation. Tested with
CamelTestSupport. No Spring involved. All To, Enrich, etc endpoints
mocked out (both subroutes and external endpoints).

Unit integration tests for the full pipelines. Here we use the Spring
Boot test harness.
To mock camel endpoints we use propertyplaceholders supplied by the
Spring Environment abstraction and @EndpointInject (mentioned earlier
in the thread):

@EndpointInject(uri = "{{DOCUMENTREGISTER_METADATA_URL}}")
private MockEndpoint documentRegisterMock;

Whereas the property/environment variable is:
DOCUMENTREGISTER_METADATA_URL=mock:documentregister

For the endpoints that we can't or won't mock using placeholders, we
use the NotifyBuilder:

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

Then use @ActiveProfiles("testenv") or -spring.profiles.active=testenv
to configure the placeholder endpoints (mock or test environment)

IMO it's not a total dealbreaker, we can still test camel route
functional behaviour for production with almost zero modification.

For me, going back to re-implementing EIP, Nope :)

On Mon, Nov 16, 2015 at 12:48 PM, Claus Ibsen <cl...@gmail.com> wrote:
> Hi
>
> You can help by logging a JIRA ticket
> http://camel.apache.org/support.html
>
> For sure camel-spring-boot is getting a lot more attraction with the
> popularity of spring-boot and hence we and Camel users will continue
> to make it better.
>
>
>
>
> On Mon, Nov 16, 2015 at 12:32 PM, Minh Tran <mi...@winning.com.au> wrote:
>> Hi
>>
>> I think the workaround has some disadvantages.
>> - It makes the routes more difficult to read. It took me a while to decipher what {{target:jms:queue}} actually meant. You might get away with it if you’re only going to mock a few endpoints but I generally mock every endpoint in my routes. I would need a placeholder for every single endpoint which is way too much work.
>> - The endpoint isn’t actually running whereas @MockEndpoints actually executes the endpoint and if I wanted to skip it, I simply switch to @MockEndpointAndSkip
>>
>> Without these features, it makes it very difficult to unit test and IMO is one of the biggest reasons why Camel rocks. It allows you to test your production routes with zero modification. I have to say this is a deal breaker and I will probably go back to plain Spring which is a shame since Spring Boot does have other lovely features.
>>
>> I fully appreciate you are putting your own time into this and am very grateful so don’t get me wrong. I hope this is constructive criticism, I apologise in advance if it isn’t. I’ll also look at how I can contribute. Thanks again.
>>
>>> On 16 Nov 2015, at 9:16 PM, Henryk Konsek <he...@gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> Yes, @MockEndpoints annotation is not supported at the moment. This is a
>>> feature that has to be coded and added to our Spring Boot module.
>>>
>>> As a workaround you can define your mocked endpoint like:
>>>
>>>  from("from...").to("{{target:jms:queue}}");
>>>
>>> And then set the target=direct:queue for the tests using the Spring Boot
>>> @IntegrationTest("target=direct:queue") annotation.
>>>
>>> Also you are more than welcome to contribute @MockEndpoints support. It is
>>> on my TODO list, but not with high priority.
>>>
>>> Cheers!
>>>
>>> pon., 16.11.2015 o 09:32 użytkownik Joakim Bjørnstad <jo...@gmail.com>
>>> napisał:
>>>
>>>> Hello,
>>>>
>>>> Doesn't seem like there is any support for @MockEndpoints and
>>>> @MockEndpointsAndSkip with Spring Boot and Camel at the moment.
>>>>
>>>> The reason for this is that the CamelSpringTestContextLoader is never
>>>> loaded. Typically you use:
>>>>
>>>> @RunWith(CamelSpringJUnit4ClassRunner.class)
>>>> @BootstrapWith(CamelTestContextBootstrapper.class)
>>>>
>>>> Together with @SpringApplicationConfiguration, it goes directly to the
>>>> SpringApplicationContextLoader and thus Camel test annotations are not
>>>> loaded.
>>>>
>>>> Please see https://issues.apache.org/jira/browse/CAMEL-7963
>>>>
>>>> On Mon, Nov 16, 2015 at 2:40 AM, Minh Tran <mi...@winning.com.au>
>>>> wrote:
>>>>> Hi
>>>>>
>>>>> I’m trying to write unit tests in camel 2.16.0 and spring boot.
>>>>>
>>>>> My route looks like
>>>>>
>>>>> from(“direct:start”).to(“direct:end”)
>>>>>
>>>>> My unit test looks like
>>>>>
>>>>> @RunWith(SpringJUnit4ClassRunner.class)
>>>>> @SpringApplicationConfiguration(classes = Config.class)
>>>>> @MockEndpoints
>>>>> public class MyUnitTest {
>>>>>
>>>>>  @Produce(uri=“direct:start”)
>>>>>  private ProducerTemplate producer;
>>>>>
>>>>>  @EndpointInject(uri=“mock:direct:end”)
>>>>>  private MockEndpoint end;
>>>>>
>>>>>        @Test
>>>>>        public void testMock() throws InterruptedException {
>>>>>                end.expectedBodiesReceived("blah");
>>>>>                producerTemplate.sendBody("blah");
>>>>>                end.assertIsSatisfied();
>>>>>        }
>>>>>
>>>>> }
>>>>>
>>>>> It looks like the direct:end bit is never mocked so the assertion fails.
>>>> It’s like @MockEndpoints is completely ignored.
>>>>>
>>>>> Is this the correct way to mock existing components when using spring
>>>> boot? Thanks.
>>>>
>>>>
>>>>
>>>> --
>>>> Kind regards
>>>> Joakim Bjørnstad
>>>>
>>> --
>>> Henryk Konsek
>>> http://about.me/hekonsek
>>
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2



-- 
Kind regards
Joakim Bjørnstad

Re: spring boot test mocks

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You can help by logging a JIRA ticket
http://camel.apache.org/support.html

For sure camel-spring-boot is getting a lot more attraction with the
popularity of spring-boot and hence we and Camel users will continue
to make it better.




On Mon, Nov 16, 2015 at 12:32 PM, Minh Tran <mi...@winning.com.au> wrote:
> Hi
>
> I think the workaround has some disadvantages.
> - It makes the routes more difficult to read. It took me a while to decipher what {{target:jms:queue}} actually meant. You might get away with it if you’re only going to mock a few endpoints but I generally mock every endpoint in my routes. I would need a placeholder for every single endpoint which is way too much work.
> - The endpoint isn’t actually running whereas @MockEndpoints actually executes the endpoint and if I wanted to skip it, I simply switch to @MockEndpointAndSkip
>
> Without these features, it makes it very difficult to unit test and IMO is one of the biggest reasons why Camel rocks. It allows you to test your production routes with zero modification. I have to say this is a deal breaker and I will probably go back to plain Spring which is a shame since Spring Boot does have other lovely features.
>
> I fully appreciate you are putting your own time into this and am very grateful so don’t get me wrong. I hope this is constructive criticism, I apologise in advance if it isn’t. I’ll also look at how I can contribute. Thanks again.
>
>> On 16 Nov 2015, at 9:16 PM, Henryk Konsek <he...@gmail.com> wrote:
>>
>> Hi,
>>
>> Yes, @MockEndpoints annotation is not supported at the moment. This is a
>> feature that has to be coded and added to our Spring Boot module.
>>
>> As a workaround you can define your mocked endpoint like:
>>
>>  from("from...").to("{{target:jms:queue}}");
>>
>> And then set the target=direct:queue for the tests using the Spring Boot
>> @IntegrationTest("target=direct:queue") annotation.
>>
>> Also you are more than welcome to contribute @MockEndpoints support. It is
>> on my TODO list, but not with high priority.
>>
>> Cheers!
>>
>> pon., 16.11.2015 o 09:32 użytkownik Joakim Bjørnstad <jo...@gmail.com>
>> napisał:
>>
>>> Hello,
>>>
>>> Doesn't seem like there is any support for @MockEndpoints and
>>> @MockEndpointsAndSkip with Spring Boot and Camel at the moment.
>>>
>>> The reason for this is that the CamelSpringTestContextLoader is never
>>> loaded. Typically you use:
>>>
>>> @RunWith(CamelSpringJUnit4ClassRunner.class)
>>> @BootstrapWith(CamelTestContextBootstrapper.class)
>>>
>>> Together with @SpringApplicationConfiguration, it goes directly to the
>>> SpringApplicationContextLoader and thus Camel test annotations are not
>>> loaded.
>>>
>>> Please see https://issues.apache.org/jira/browse/CAMEL-7963
>>>
>>> On Mon, Nov 16, 2015 at 2:40 AM, Minh Tran <mi...@winning.com.au>
>>> wrote:
>>>> Hi
>>>>
>>>> I’m trying to write unit tests in camel 2.16.0 and spring boot.
>>>>
>>>> My route looks like
>>>>
>>>> from(“direct:start”).to(“direct:end”)
>>>>
>>>> My unit test looks like
>>>>
>>>> @RunWith(SpringJUnit4ClassRunner.class)
>>>> @SpringApplicationConfiguration(classes = Config.class)
>>>> @MockEndpoints
>>>> public class MyUnitTest {
>>>>
>>>>  @Produce(uri=“direct:start”)
>>>>  private ProducerTemplate producer;
>>>>
>>>>  @EndpointInject(uri=“mock:direct:end”)
>>>>  private MockEndpoint end;
>>>>
>>>>        @Test
>>>>        public void testMock() throws InterruptedException {
>>>>                end.expectedBodiesReceived("blah");
>>>>                producerTemplate.sendBody("blah");
>>>>                end.assertIsSatisfied();
>>>>        }
>>>>
>>>> }
>>>>
>>>> It looks like the direct:end bit is never mocked so the assertion fails.
>>> It’s like @MockEndpoints is completely ignored.
>>>>
>>>> Is this the correct way to mock existing components when using spring
>>> boot? Thanks.
>>>
>>>
>>>
>>> --
>>> Kind regards
>>> Joakim Bjørnstad
>>>
>> --
>> Henryk Konsek
>> http://about.me/hekonsek
>



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: spring boot test mocks

Posted by Minh Tran <mi...@winning.com.au>.
Hi

I think the workaround has some disadvantages. 
- It makes the routes more difficult to read. It took me a while to decipher what {{target:jms:queue}} actually meant. You might get away with it if you’re only going to mock a few endpoints but I generally mock every endpoint in my routes. I would need a placeholder for every single endpoint which is way too much work.
- The endpoint isn’t actually running whereas @MockEndpoints actually executes the endpoint and if I wanted to skip it, I simply switch to @MockEndpointAndSkip

Without these features, it makes it very difficult to unit test and IMO is one of the biggest reasons why Camel rocks. It allows you to test your production routes with zero modification. I have to say this is a deal breaker and I will probably go back to plain Spring which is a shame since Spring Boot does have other lovely features. 

I fully appreciate you are putting your own time into this and am very grateful so don’t get me wrong. I hope this is constructive criticism, I apologise in advance if it isn’t. I’ll also look at how I can contribute. Thanks again.
 
> On 16 Nov 2015, at 9:16 PM, Henryk Konsek <he...@gmail.com> wrote:
> 
> Hi,
> 
> Yes, @MockEndpoints annotation is not supported at the moment. This is a
> feature that has to be coded and added to our Spring Boot module.
> 
> As a workaround you can define your mocked endpoint like:
> 
>  from("from...").to("{{target:jms:queue}}");
> 
> And then set the target=direct:queue for the tests using the Spring Boot
> @IntegrationTest("target=direct:queue") annotation.
> 
> Also you are more than welcome to contribute @MockEndpoints support. It is
> on my TODO list, but not with high priority.
> 
> Cheers!
> 
> pon., 16.11.2015 o 09:32 użytkownik Joakim Bjørnstad <jo...@gmail.com>
> napisał:
> 
>> Hello,
>> 
>> Doesn't seem like there is any support for @MockEndpoints and
>> @MockEndpointsAndSkip with Spring Boot and Camel at the moment.
>> 
>> The reason for this is that the CamelSpringTestContextLoader is never
>> loaded. Typically you use:
>> 
>> @RunWith(CamelSpringJUnit4ClassRunner.class)
>> @BootstrapWith(CamelTestContextBootstrapper.class)
>> 
>> Together with @SpringApplicationConfiguration, it goes directly to the
>> SpringApplicationContextLoader and thus Camel test annotations are not
>> loaded.
>> 
>> Please see https://issues.apache.org/jira/browse/CAMEL-7963
>> 
>> On Mon, Nov 16, 2015 at 2:40 AM, Minh Tran <mi...@winning.com.au>
>> wrote:
>>> Hi
>>> 
>>> I’m trying to write unit tests in camel 2.16.0 and spring boot.
>>> 
>>> My route looks like
>>> 
>>> from(“direct:start”).to(“direct:end”)
>>> 
>>> My unit test looks like
>>> 
>>> @RunWith(SpringJUnit4ClassRunner.class)
>>> @SpringApplicationConfiguration(classes = Config.class)
>>> @MockEndpoints
>>> public class MyUnitTest {
>>> 
>>>  @Produce(uri=“direct:start”)
>>>  private ProducerTemplate producer;
>>> 
>>>  @EndpointInject(uri=“mock:direct:end”)
>>>  private MockEndpoint end;
>>> 
>>>        @Test
>>>        public void testMock() throws InterruptedException {
>>>                end.expectedBodiesReceived("blah");
>>>                producerTemplate.sendBody("blah");
>>>                end.assertIsSatisfied();
>>>        }
>>> 
>>> }
>>> 
>>> It looks like the direct:end bit is never mocked so the assertion fails.
>> It’s like @MockEndpoints is completely ignored.
>>> 
>>> Is this the correct way to mock existing components when using spring
>> boot? Thanks.
>> 
>> 
>> 
>> --
>> Kind regards
>> Joakim Bjørnstad
>> 
> -- 
> Henryk Konsek
> http://about.me/hekonsek


Re: spring boot test mocks

Posted by Henryk Konsek <he...@gmail.com>.
Hi,

Yes, @MockEndpoints annotation is not supported at the moment. This is a
feature that has to be coded and added to our Spring Boot module.

As a workaround you can define your mocked endpoint like:

  from("from...").to("{{target:jms:queue}}");

And then set the target=direct:queue for the tests using the Spring Boot
@IntegrationTest("target=direct:queue") annotation.

Also you are more than welcome to contribute @MockEndpoints support. It is
on my TODO list, but not with high priority.

Cheers!

pon., 16.11.2015 o 09:32 użytkownik Joakim Bjørnstad <jo...@gmail.com>
napisał:

> Hello,
>
> Doesn't seem like there is any support for @MockEndpoints and
> @MockEndpointsAndSkip with Spring Boot and Camel at the moment.
>
> The reason for this is that the CamelSpringTestContextLoader is never
> loaded. Typically you use:
>
> @RunWith(CamelSpringJUnit4ClassRunner.class)
> @BootstrapWith(CamelTestContextBootstrapper.class)
>
> Together with @SpringApplicationConfiguration, it goes directly to the
> SpringApplicationContextLoader and thus Camel test annotations are not
> loaded.
>
> Please see https://issues.apache.org/jira/browse/CAMEL-7963
>
> On Mon, Nov 16, 2015 at 2:40 AM, Minh Tran <mi...@winning.com.au>
> wrote:
> > Hi
> >
> > I’m trying to write unit tests in camel 2.16.0 and spring boot.
> >
> > My route looks like
> >
> > from(“direct:start”).to(“direct:end”)
> >
> > My unit test looks like
> >
> > @RunWith(SpringJUnit4ClassRunner.class)
> > @SpringApplicationConfiguration(classes = Config.class)
> > @MockEndpoints
> > public class MyUnitTest {
> >
> >   @Produce(uri=“direct:start”)
> >   private ProducerTemplate producer;
> >
> >   @EndpointInject(uri=“mock:direct:end”)
> >   private MockEndpoint end;
> >
> >         @Test
> >         public void testMock() throws InterruptedException {
> >                 end.expectedBodiesReceived("blah");
> >                 producerTemplate.sendBody("blah");
> >                 end.assertIsSatisfied();
> >         }
> >
> > }
> >
> > It looks like the direct:end bit is never mocked so the assertion fails.
> It’s like @MockEndpoints is completely ignored.
> >
> > Is this the correct way to mock existing components when using spring
> boot? Thanks.
>
>
>
> --
> Kind regards
> Joakim Bjørnstad
>
-- 
Henryk Konsek
http://about.me/hekonsek

Re: spring boot test mocks

Posted by Joakim Bjørnstad <jo...@gmail.com>.
Hello,

Doesn't seem like there is any support for @MockEndpoints and
@MockEndpointsAndSkip with Spring Boot and Camel at the moment.

The reason for this is that the CamelSpringTestContextLoader is never
loaded. Typically you use:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)

Together with @SpringApplicationConfiguration, it goes directly to the
SpringApplicationContextLoader and thus Camel test annotations are not
loaded.

Please see https://issues.apache.org/jira/browse/CAMEL-7963

On Mon, Nov 16, 2015 at 2:40 AM, Minh Tran <mi...@winning.com.au> wrote:
> Hi
>
> I’m trying to write unit tests in camel 2.16.0 and spring boot.
>
> My route looks like
>
> from(“direct:start”).to(“direct:end”)
>
> My unit test looks like
>
> @RunWith(SpringJUnit4ClassRunner.class)
> @SpringApplicationConfiguration(classes = Config.class)
> @MockEndpoints
> public class MyUnitTest {
>
>   @Produce(uri=“direct:start”)
>   private ProducerTemplate producer;
>
>   @EndpointInject(uri=“mock:direct:end”)
>   private MockEndpoint end;
>
>         @Test
>         public void testMock() throws InterruptedException {
>                 end.expectedBodiesReceived("blah");
>                 producerTemplate.sendBody("blah");
>                 end.assertIsSatisfied();
>         }
>
> }
>
> It looks like the direct:end bit is never mocked so the assertion fails. It’s like @MockEndpoints is completely ignored.
>
> Is this the correct way to mock existing components when using spring boot? Thanks.



-- 
Kind regards
Joakim Bjørnstad