camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Carman <jcar...@carmanconsulting.com>
Subject Re: Camel JUnit Extensions + EasyMock
Date Mon, 02 Jul 2012 10:28:20 GMT
There are times when your route needs to call a service of some sort.  This
is when I use mock objects.
On Jul 2, 2012 5:06 AM, "Henryk Konsek" <hekonsek@gmail.com> wrote:

> Hi all,
>
> > Typically I would use EasyMock for this. Is there any problems in using
> > EasyMock with the Camel JUnit extensions? Does Camel provide another
> > approach for this problem?
>
> Of course you can add mocked bean into registry. It will work like a
> charm with EasyMock, Mockito and so forth.
>
> SimpleRegistry registry = new SimpleRegistry();
> registry.put("MyProcessor", EasyMock.createMock(MyProcessor.class));
> CamelContext context = new DefaultCamelContext(registry);
>
> But I'll personally prefer to stick to more module way of testing my
> routes:
>
> 1) Test Processor in separated Unit test.
>
> 2) Model your production routes to use Direct [1] endpoints:
>
> from("MyEndpoint").to("direct:process");
>
> from("direct:process").to("bean:MyProcessor").to("direct:route");
>
> from("direct:route").choice() ...
>   when(header("header").equals("foo)).
>     to("direct:fooChoice") ...
>
> 3) add Mock [2] endpoints to your tests:
>
> from("direct:fooChoice").to("mock:fooChoice");
>
> 3) Then test headers values directly:
>
> @Test
> public void shouldRouteToFoo() {
>   // Given
>   String message = "message";
>
>   // When
>   template.sendBodyAndHeader("direct:route", "header", "foo", message);
>
>   // Then
>   MockEndpoint endpoint = getMockEndpoint("mock:fooChoice");
>   // make some assertions on mock endpoint here
> }
>
> This approach is much more maintainable for large applications.
> Putting mocks onto monolithic routes leads to cluttered routing test
> code. This is just my opinion of course - based on my experience, not
> on official Camel guidelines.
>
> Best regards.
>
> [1] http://camel.apache.org/direct.html
> [2] http://camel.apache.org/mock.html
>
> --
> Henryk Konsek
> http://henryk-konsek.blogspot.com
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message