You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by shyenuganti <sh...@gmail.com> on 2013/02/26 21:01:56 UTC

Issue Replacing a component with Mock in Camel Context

Hi ,

Here is a sample test for my Camel route. 

I am overriding the camel context to replace a JMS Endpoint  with a Mock.
And setting the expectations on the route and trying to assert it.  But this
test is failing. Can anyone see a problem in my test ? Instead of adding a
mock to my production route, I am changing the JMS to Mock on the fly in the
unit test. Can this be done ? If yes, What is wrong in my unit test ?

Please help.



public class EventListenerRouteMockTest extends CamelSpringTestSupport {
	
	   	@EndpointInject(uri = "jms:queue/jbossEventQueue")
	    protected MockEndpoint mock;

	    @EndpointInject(uri = "jms:queue/jbossEventQueue")
	    Endpoint endPoint;
	    
	    @Produce(uri = "direct:eventMessageList")
	    protected ProducerTemplate producer;

	    @Test
	    public void testSendMatchingMessage() throws Exception {
	        
	    	System.out.println("Starting the test ...");
	    	EventQueue event = new EventQueue();
			event.setEventMessage("TestMessage");
			
			List<EventQueue> expectedBodies = new ArrayList<EventQueue>();
			expectedBodies.add(event);
			
			mock.setExpectedMessageCount(1);
			mock.expectedBodiesReceived(expectedBodies);
			
			producer.sendBody("direct:eventMessageList", event);
			mock.assertIsSatisfied();
			
			
			
	    }

	    @Override
	    protected RouteBuilder createRouteBuilder() {
	      
	    	System.out.println("Creating RouteBuilder ...");
	    	//return new EventListenerRoute();
	    	return new RouteBuilder() {
	            public void configure() {
	            	from("direct:eventMessageList").id("PseudoEventQueue")
	    			.to("log:{exchange}?level=INFO")
	    			.to("jms:queue");
	            }
	        };
	    }

	    @Override
	    protected CamelContext createCamelContext() throws Exception {
	    	System.out.println("Overriding JMS with Mock ...");
	    	CamelContext context = super.createCamelContext();
		 	context.addComponent("jms", context.getComponent("mock"));
		    return context;
	    }
	    
		@Override
		protected AbstractApplicationContext createApplicationContext() {
			System.out.println("createApplicationContext() method");
			return new
ClassPathXmlApplicationContext("META-INF/spring/testCamelContext.xml");
		}
	}




--
View this message in context: http://camel.465427.n5.nabble.com/Issue-Replacing-a-component-with-Mock-in-Camel-Context-tp5728195.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Issue Replacing a component with Mock in Camel Context

Posted by Christian Müller <ch...@gmail.com>.
First of all, you should use
from("direct:eventMessageList").routeId("PseudoEventQueue")
instead of
from("direct:eventMessageList").id("PseudoEventQueue")

And in your test method, you can do
        context.getRouteDefinition("PseudoEventQueue").adviceWith(context,
new RouteBuilder() {
            public void configure() throws Exception {
                interceptSendToEndpoint("jms:queue:foo")
                    .skipSendToOriginalEndpoint()
                    .to("mock:foo");
            }
        });

as described here:
http://camel.apache.org/advicewith.html

Will this work?

Best,
Christian

On Tue, Feb 26, 2013 at 10:27 PM, shyenuganti <sh...@gmail.com> wrote:

> context.resolveEndpoint is not availble on both the regular CamelContext or
> the ModelCamelContext in 2.10.3. Is the method deprecated?
>
> I am using @EndpointInject to get the resolve the endpoint.
>
> But still the assertion fails in my case although everything seems to work.
> Is there any thing that I doing wrong in my example above?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Issue-Replacing-a-component-with-Mock-in-Camel-Context-tp5728195p5728200.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--

Re: Issue Replacing a component with Mock in Camel Context

Posted by shyenuganti <sh...@gmail.com>.
context.resolveEndpoint is not availble on both the regular CamelContext or
the ModelCamelContext in 2.10.3. Is the method deprecated? 

I am using @EndpointInject to get the resolve the endpoint. 

But still the assertion fails in my case although everything seems to work.
Is there any thing that I doing wrong in my example above?



--
View this message in context: http://camel.465427.n5.nabble.com/Issue-Replacing-a-component-with-Mock-in-Camel-Context-tp5728195p5728200.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Issue Replacing a component with Mock in Camel Context

Posted by Christian Müller <ch...@gmail.com>.
This is a good source: http://camel.apache.org/mock.html

Sent from a mobile device
Am 26.02.2013 12:02 schrieb "shyenuganti" <sh...@gmail.com>:

> Hi ,
>
> Here is a sample test for my Camel route.
>
> I am overriding the camel context to replace a JMS Endpoint  with a Mock.
> And setting the expectations on the route and trying to assert it.  But
> this
> test is failing. Can anyone see a problem in my test ? Instead of adding a
> mock to my production route, I am changing the JMS to Mock on the fly in
> the
> unit test. Can this be done ? If yes, What is wrong in my unit test ?
>
> Please help.
>
>
>
> public class EventListenerRouteMockTest extends CamelSpringTestSupport {
>
>                 @EndpointInject(uri = "jms:queue/jbossEventQueue")
>             protected MockEndpoint mock;
>
>             @EndpointInject(uri = "jms:queue/jbossEventQueue")
>             Endpoint endPoint;
>
>             @Produce(uri = "direct:eventMessageList")
>             protected ProducerTemplate producer;
>
>             @Test
>             public void testSendMatchingMessage() throws Exception {
>
>                 System.out.println("Starting the test ...");
>                 EventQueue event = new EventQueue();
>                         event.setEventMessage("TestMessage");
>
>                         List<EventQueue> expectedBodies = new
> ArrayList<EventQueue>();
>                         expectedBodies.add(event);
>
>                         mock.setExpectedMessageCount(1);
>                         mock.expectedBodiesReceived(expectedBodies);
>
>                         producer.sendBody("direct:eventMessageList",
> event);
>                         mock.assertIsSatisfied();
>
>
>
>             }
>
>             @Override
>             protected RouteBuilder createRouteBuilder() {
>
>                 System.out.println("Creating RouteBuilder ...");
>                 //return new EventListenerRoute();
>                 return new RouteBuilder() {
>                     public void configure() {
>
> from("direct:eventMessageList").id("PseudoEventQueue")
>                                 .to("log:{exchange}?level=INFO")
>                                 .to("jms:queue");
>                     }
>                 };
>             }
>
>             @Override
>             protected CamelContext createCamelContext() throws Exception {
>                 System.out.println("Overriding JMS with Mock ...");
>                 CamelContext context = super.createCamelContext();
>                         context.addComponent("jms",
> context.getComponent("mock"));
>                     return context;
>             }
>
>                 @Override
>                 protected AbstractApplicationContext
> createApplicationContext() {
>                         System.out.println("createApplicationContext()
> method");
>                         return new
> ClassPathXmlApplicationContext("META-INF/spring/testCamelContext.xml");
>                 }
>         }
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Issue-Replacing-a-component-with-Mock-in-Camel-Context-tp5728195.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>