You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Bradford Fisher <br...@bradfordfisher.com> on 2019/01/18 15:42:39 UTC

Mocking Beans

Does Camel have some built in functionality to replace a Bean within a
Route while maintaining the method called on the bean?

For instance, given the RouteBuilder below, I would like to replace myBean
with myMockBean during testing. I've considered using AdviceWith, but that
seams to replace the entire Bean Node rather than just the instance of the
class used by the Bean Node.

public class MyRouteBuilder extends RouteBuilder
{
    @BeanInject
    private MyBean myBean;

    @Override
    public void configure() throws Exception
    {
        from("direct:myEndpoint")
            .bean(myBean, "myMethod")
            .id("myBeanId");
    }
}

Re: Mocking Beans

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

Yeah you can also have getter/setter for the bean and then set a new
bean in the route builder class before starting the test.
Or as suggested by Luiz you can inject other beans from the @Autowried
@BeanInject and what else annotations are there.
And for advice with, you can also use its replace with, and then set
up another node  that calls another bean.


On Fri, Jan 18, 2019 at 7:45 PM Bradford Fisher
<br...@bradfordfisher.com> wrote:
>
> Thanks Luiz! Constructor injection should work for us. Just wanted to make
> sure there wasn't something built in to Camel!
>
> On Fri, Jan 18, 2019 at 1:09 PM Luiz Eduardo Valmont <
> valmont@alis-sol.com.br> wrote:
>
> > I confess I'm not familiar with @BeanInject, but if it's similar in purpose
> > as @Autowired from Spring, then I'd suggest using constructor injection.
> > That way you can create your object with your mocked bean.
> >
> > Personally I do it like that in unit testing.
> >
> > HTH
> >
> > On Fri, 18 Jan 2019, 13:42 Bradford Fisher <bradford@bradfordfisher.com
> > wrote:
> >
> > > Does Camel have some built in functionality to replace a Bean within a
> > > Route while maintaining the method called on the bean?
> > >
> > > For instance, given the RouteBuilder below, I would like to replace
> > myBean
> > > with myMockBean during testing. I've considered using AdviceWith, but
> > that
> > > seams to replace the entire Bean Node rather than just the instance of
> > the
> > > class used by the Bean Node.
> > >
> > > public class MyRouteBuilder extends RouteBuilder
> > > {
> > >     @BeanInject
> > >     private MyBean myBean;
> > >
> > >     @Override
> > >     public void configure() throws Exception
> > >     {
> > >         from("direct:myEndpoint")
> > >             .bean(myBean, "myMethod")
> > >             .id("myBeanId");
> > >     }
> > > }
> > >
> >



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

Re: Mocking Beans

Posted by Bradford Fisher <br...@bradfordfisher.com>.
Thanks Luiz! Constructor injection should work for us. Just wanted to make
sure there wasn't something built in to Camel!

On Fri, Jan 18, 2019 at 1:09 PM Luiz Eduardo Valmont <
valmont@alis-sol.com.br> wrote:

> I confess I'm not familiar with @BeanInject, but if it's similar in purpose
> as @Autowired from Spring, then I'd suggest using constructor injection.
> That way you can create your object with your mocked bean.
>
> Personally I do it like that in unit testing.
>
> HTH
>
> On Fri, 18 Jan 2019, 13:42 Bradford Fisher <bradford@bradfordfisher.com
> wrote:
>
> > Does Camel have some built in functionality to replace a Bean within a
> > Route while maintaining the method called on the bean?
> >
> > For instance, given the RouteBuilder below, I would like to replace
> myBean
> > with myMockBean during testing. I've considered using AdviceWith, but
> that
> > seams to replace the entire Bean Node rather than just the instance of
> the
> > class used by the Bean Node.
> >
> > public class MyRouteBuilder extends RouteBuilder
> > {
> >     @BeanInject
> >     private MyBean myBean;
> >
> >     @Override
> >     public void configure() throws Exception
> >     {
> >         from("direct:myEndpoint")
> >             .bean(myBean, "myMethod")
> >             .id("myBeanId");
> >     }
> > }
> >
>

Re: Mocking Beans

Posted by Luiz Eduardo Valmont <va...@alis-sol.com.br>.
I confess I'm not familiar with @BeanInject, but if it's similar in purpose
as @Autowired from Spring, then I'd suggest using constructor injection.
That way you can create your object with your mocked bean.

Personally I do it like that in unit testing.

HTH

On Fri, 18 Jan 2019, 13:42 Bradford Fisher <bradford@bradfordfisher.com
wrote:

> Does Camel have some built in functionality to replace a Bean within a
> Route while maintaining the method called on the bean?
>
> For instance, given the RouteBuilder below, I would like to replace myBean
> with myMockBean during testing. I've considered using AdviceWith, but that
> seams to replace the entire Bean Node rather than just the instance of the
> class used by the Bean Node.
>
> public class MyRouteBuilder extends RouteBuilder
> {
>     @BeanInject
>     private MyBean myBean;
>
>     @Override
>     public void configure() throws Exception
>     {
>         from("direct:myEndpoint")
>             .bean(myBean, "myMethod")
>             .id("myBeanId");
>     }
> }
>