You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by whelmingbytes <sp...@gmail.com> on 2013/02/13 18:42:39 UTC

Help with mocking a filter bean method call

Hello,

I'm somewhat new to camel so sorry if I am not explaining the concepts very
well. I have a route that looks something like:

from('direct:start').filter().method('bean',
'matches').to('mock:endpoint').stop()

In my test, I am trying to mock out the method 'matches' call using mockito.
I have successfully mocked route beans before with:

routeDefinition.adviceWith(context,new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        weaveById(camelId).replace().bean(bean, 'matches');
    }
});

But, I am not sure how to go about swapping out my filters bean method call
with my mock. I have a gist here with a test if anyone is willing to take a
look or point me to some examples of this:

https://gist.github.com/whelmingbytes/446d0650b47ae082b626

Thanks!

-Spencer



--
View this message in context: http://camel.465427.n5.nabble.com/Help-with-mocking-a-filter-bean-method-call-tp5727543.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help with mocking a filter bean method call

Posted by Henryk Konsek <he...@gmail.com>.
> I will be going the route of using the spring based registry. Looks like
> exactly what I need and have had good luck with it so far.

Great to hear that. Feel free to drop us a line if you need any further advice.

-- 
Henryk Konsek
http://henryk-konsek.blogspot.com

Re: Help with mocking a filter bean method call

Posted by whelmingbytes <sp...@gmail.com>.
Thanks for the response. I agree I shouldn't put mocks into our int tests,
but the lovely code with this in it, I inherited.

I will be going the route of using the spring based registry. Looks like
exactly what I need and have had good luck with it so far.

Thanks again!



--
View this message in context: http://camel.465427.n5.nabble.com/Help-with-mocking-a-filter-bean-method-call-tp5727543p5727665.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help with mocking a filter bean method call

Posted by Henryk Konsek <he...@gmail.com>.
> In my actual test, my bean is loaded up with spring
> component scan (most of the tests that use this are integration tests)

Actually I would not advice putting Mockito mocks into integration
tests, as the latter approach makes your test hard to maintain and
control.

But of course you can configure your Spring context scanning to pick
the right bean depending on the execution context. One way to achieve
this is via Spring Profiles [1]. The easiest way to put Mockito mock
into your Spring context is via Spring Java Configuration as
@Configurations are detected by the Spring classpath scanning:

@Configuration
public class MockConfiguration {

  @Bean
  public MyComponent bean() {
    return Mockito.mock(MyComponent.class);
  }

}

You can also consider using Arquillian [2] for composing isolated test
classpath which will contain your MockConfiguration (if you don't like
or can't use Spring Profiles). Arquillian is a fine piece of software
and I will introduce official Camel and ServiceMix support for it in
the future. In the meantime you can benefit even from vanilla
Arquillian setup.

Word of advice - if you plan to introduce Mocks into your tests, try
to do it on the registry [3] level (Spring in your particular case),
not by hacking the routes with AOP or interceptors. This will make
your tests much easier to maintain. Also tests running routes not
affected by interceptors are more likely to reflect your production
environment.

[1] http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile
[2] http://arquillian.org
[3] http://camel.apache.org/registry.html

--
Henryk Konsek
http://henryk-konsek.blogspot.com

Re: Help with mocking a filter bean method call

Posted by whelmingbytes <sp...@gmail.com>.
Thanks for the quick reply!

This will work for this example, but I only created this example for
simplicity sake. In my actual test, my bean is loaded up with spring
component scan (most of the tests that use this are integration tests) and
the test is initialized with overriding the createApplicationContext()
method.

Can I replace a bean that is already in the context? We have large test
context file and without creating a new mocked one for my filter test, is
there any better way to do this? I was hoping to go down the path of using
advice like I do with our replace().bean() calls.

Thanks again!




--
View this message in context: http://camel.465427.n5.nabble.com/Help-with-mocking-a-filter-bean-method-call-tp5727543p5727552.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help with mocking a filter bean method call

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

Welcome to our community :) .

> from('direct:start').filter().method('bean',
> 'matches').to('mock:endpoint').stop()
>
> In my test, I am trying to mock out the method 'matches' call using mockito.

The approach of mine choice would be to register Mockito mock in the
Camel registry under the 'bean' name.

MyBean myBean = Mockito.mock(MyBean.class);
...
SimpleRegistry registry = new SimpleRegistry();
registry.put("bean", myBean);
CamelContext ctx = new DefaultCamelContext(registry);
...
ctx.createProducerTemplate().sendBody("direct:start", "Mockito FTW!");
...
Mockito.verify(myBean).matches();

AdviceWith seems to be a too big cannon for shooting the tiny little mock :) .

Best regards.

--
Henryk Konsek
http://henryk-konsek.blogspot.com