You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "Gershaw, Geoffrey" <ge...@credit-suisse.com> on 2012/05/24 23:31:20 UTC

Unit testing a method with Exchange as a Param

Hello all,

I am trying to unit test the following method, which is essentially the
Sending to multiple JMS destinations from one endpoint example from the
camel website.  Since the Exchange object is a param and contains the
result, I'm having trouble Mocking it. The below test fails at the ***
line. actualTopicName is null :( . Essentially all the publish method is
doing, is adding headers to the In Msg of the Exchange object. 

Appreciate the advice.

Geoff



	@Test
	public void testSingleTopicPublish(){
		MarketData md = createMarketData();
		 
		Exchange exchange = mock(Exchange.class);
		Message inMsg = mock(Message.class);
		
		
		when(exchange.getIn()).thenReturn(inMsg);
		when(inMsg.getBody(Publishable.class)).thenReturn(md);


		String singleTopic = "TEST.TOPIC";
		destinationResolver = new
JMSDestinationResolver(singleTopic,PublishType.SINGLE_DESTINATION);
		destinationResolver.publish(exchange);

		Message actualInMsg = exchange.getIn();
		String actualTopicName = (String)
actualInMsg.getHeader(JmsConstants.JMS_DESTINATION_NAME); 

		assertEquals(singleTopic, actualTopicName);
************************************


	}


	/**
	 * Send exchange to a JMS Destination determined at Runtime
	 * @param exchange
	 */
	public void publish(Exchange exchange){
		Publishable publishable=
exchange.getIn().getBody(Publishable.class);
		Map<String,String> origMsgHeaders =
publishable.getPublishProperties();
		Map<String,Object> msgHeaders = new HashMap<String,
Object>(origMsgHeaders);
		String destinationName = null;

		//If we are publishing on muiltiple topics and there is
no destinationPrefix can't publish this msg
		if(publishType == PublishType.MULTIPLE_DESTINATIONS){
	
if(!StringUtils.isEmpty(publishable.getPublishTopicName())){
				destinationName = String.format("%s.%s",
destinationPrefix, publishable.getPublishTopicName());
			}else{
				log.error("Empty topicName for msg: " +
publishable);
			}
		}
		else{
			//The we are publishing on 1 topic, grab it from
config
			destinationName = destinationPrefix;
		}

		if(log.isDebugEnabled()){
			log.debug("Destination is " + destinationName);
		}
		msgHeaders.put(JmsConstants.JMS_DESTINATION_NAME,
destinationName);
		exchange.getIn().setHeaders(msgHeaders);
	}

=============================================================================== 
Please access the attached hyperlink for an important electronic communications disclaimer: 
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html 
=============================================================================== 


RE: Unit testing a method with Exchange as a Param

Posted by "Gershaw, Geoffrey" <ge...@credit-suisse.com>.
Yeah, I figured that one out

Geoffrey A. Gershaw
Credit eTrading Dev - RDO
1 919 994 6412


-----Original Message-----
From: Christian Müller [mailto:christian.mueller@gmail.com] 
Sent: Friday, May 25, 2012 9:44 AM
To: users@camel.apache.org
Subject: RE: Unit testing a method with Exchange as a Param

You are welcome. And replace in my last post
CamelContext ctx = new CamelContext();
with
CamelContext ctx = new DefaultCamelContext();

It was a bit too late yesterday... ;-)

Best,
Chrostian
Sent from a mobile device
Am 25.05.2012 14:15 schrieb "Gershaw, Geoffrey" <
geoffrey.gershaw@credit-suisse.com>:

> Christian,
>
> You are a gentleman and a scholar. I need to be a better Camel Rider. I
> didn't realize how easy it is to Unit test. I thought I'd have to launch
> the whole Spring dependency.
>
> Thanks again
>
> Geoffrey A. Gershaw
> Credit eTrading Dev - RDO
> 1 919 994 6412
>
>
> -----Original Message-----
> From: Christian Müller [mailto:christian.mueller@gmail.com]
> Sent: Thursday, May 24, 2012 6:40 PM
> To: users@camel.apache.org
> Subject: Re: Unit testing a method with Exchange as a Param
>
> I wouldn't mock the Exchange:
>
> CamelContext ctx = new CamelContext();
> Exchange ex = new DefaultExchange(ctx);
>
> // your code...
>
> Best,
> Christian
>
> On Thu, May 24, 2012 at 11:31 PM, Gershaw, Geoffrey <
> geoffrey.gershaw@credit-suisse.com> wrote:
>
> > Hello all,
> >
> > I am trying to unit test the following method, which is essentially the
> > Sending to multiple JMS destinations from one endpoint example from the
> > camel website.  Since the Exchange object is a param and contains the
> > result, I'm having trouble Mocking it. The below test fails at the ***
> > line. actualTopicName is null :( . Essentially all the publish method is
> > doing, is adding headers to the In Msg of the Exchange object.
> >
> > Appreciate the advice.
> >
> > Geoff
> >
> >
> >
> >        @Test
> >        public void testSingleTopicPublish(){
> >                MarketData md = createMarketData();
> >
> >                Exchange exchange = mock(Exchange.class);
> >                Message inMsg = mock(Message.class);
> >
> >
> >                when(exchange.getIn()).thenReturn(inMsg);
> >                when(inMsg.getBody(Publishable.class)).thenReturn(md);
> >
> >
> >                String singleTopic = "TEST.TOPIC";
> >                destinationResolver = new
> > JMSDestinationResolver(singleTopic,PublishType.SINGLE_DESTINATION);
> >                destinationResolver.publish(exchange);
> >
> >                Message actualInMsg = exchange.getIn();
> >                String actualTopicName = (String)
> > actualInMsg.getHeader(JmsConstants.JMS_DESTINATION_NAME);
> >
> >                assertEquals(singleTopic, actualTopicName);
> > ************************************
> >
> >
> >        }
> >
> >
> >        /**
> >         * Send exchange to a JMS Destination determined at Runtime
> >         * @param exchange
> >         */
> >        public void publish(Exchange exchange){
> >                Publishable publishable=
> > exchange.getIn().getBody(Publishable.class);
> >                Map<String,String> origMsgHeaders =
> > publishable.getPublishProperties();
> >                Map<String,Object> msgHeaders = new HashMap<String,
> > Object>(origMsgHeaders);
> >                String destinationName = null;
> >
> >                //If we are publishing on muiltiple topics and there is
> > no destinationPrefix can't publish this msg
> >                if(publishType == PublishType.MULTIPLE_DESTINATIONS){
> >
> > if(!StringUtils.isEmpty(publishable.getPublishTopicName())){
> >                                destinationName = String.format("%s.%s",
> > destinationPrefix, publishable.getPublishTopicName());
> >                        }else{
> >                                log.error("Empty topicName for msg: " +
> > publishable);
> >                        }
> >                }
> >                else{
> >                        //The we are publishing on 1 topic, grab it from
> > config
> >                        destinationName = destinationPrefix;
> >                }
> >
> >                if(log.isDebugEnabled()){
> >                        log.debug("Destination is " + destinationName);
> >                }
> >                msgHeaders.put(JmsConstants.JMS_DESTINATION_NAME,
> > destinationName);
> >                exchange.getIn().setHeaders(msgHeaders);
> >        }
> >
> >
> >
> ===============================================================================
> > Please access the attached hyperlink for an important electronic
> > communications disclaimer:
> > http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> >
> >
> ===============================================================================
> >
> >
>
>
> ===============================================================================
> Please access the attached hyperlink for an important electronic
> communications disclaimer:
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
> ===============================================================================
>
>

=============================================================================== 
Please access the attached hyperlink for an important electronic communications disclaimer: 
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html 
=============================================================================== 


RE: Unit testing a method with Exchange as a Param

Posted by Christian Müller <ch...@gmail.com>.
You are welcome. And replace in my last post
CamelContext ctx = new CamelContext();
with
CamelContext ctx = new DefaultCamelContext();

It was a bit too late yesterday... ;-)

Best,
Chrostian
Sent from a mobile device
Am 25.05.2012 14:15 schrieb "Gershaw, Geoffrey" <
geoffrey.gershaw@credit-suisse.com>:

> Christian,
>
> You are a gentleman and a scholar. I need to be a better Camel Rider. I
> didn't realize how easy it is to Unit test. I thought I'd have to launch
> the whole Spring dependency.
>
> Thanks again
>
> Geoffrey A. Gershaw
> Credit eTrading Dev - RDO
> 1 919 994 6412
>
>
> -----Original Message-----
> From: Christian Müller [mailto:christian.mueller@gmail.com]
> Sent: Thursday, May 24, 2012 6:40 PM
> To: users@camel.apache.org
> Subject: Re: Unit testing a method with Exchange as a Param
>
> I wouldn't mock the Exchange:
>
> CamelContext ctx = new CamelContext();
> Exchange ex = new DefaultExchange(ctx);
>
> // your code...
>
> Best,
> Christian
>
> On Thu, May 24, 2012 at 11:31 PM, Gershaw, Geoffrey <
> geoffrey.gershaw@credit-suisse.com> wrote:
>
> > Hello all,
> >
> > I am trying to unit test the following method, which is essentially the
> > Sending to multiple JMS destinations from one endpoint example from the
> > camel website.  Since the Exchange object is a param and contains the
> > result, I'm having trouble Mocking it. The below test fails at the ***
> > line. actualTopicName is null :( . Essentially all the publish method is
> > doing, is adding headers to the In Msg of the Exchange object.
> >
> > Appreciate the advice.
> >
> > Geoff
> >
> >
> >
> >        @Test
> >        public void testSingleTopicPublish(){
> >                MarketData md = createMarketData();
> >
> >                Exchange exchange = mock(Exchange.class);
> >                Message inMsg = mock(Message.class);
> >
> >
> >                when(exchange.getIn()).thenReturn(inMsg);
> >                when(inMsg.getBody(Publishable.class)).thenReturn(md);
> >
> >
> >                String singleTopic = "TEST.TOPIC";
> >                destinationResolver = new
> > JMSDestinationResolver(singleTopic,PublishType.SINGLE_DESTINATION);
> >                destinationResolver.publish(exchange);
> >
> >                Message actualInMsg = exchange.getIn();
> >                String actualTopicName = (String)
> > actualInMsg.getHeader(JmsConstants.JMS_DESTINATION_NAME);
> >
> >                assertEquals(singleTopic, actualTopicName);
> > ************************************
> >
> >
> >        }
> >
> >
> >        /**
> >         * Send exchange to a JMS Destination determined at Runtime
> >         * @param exchange
> >         */
> >        public void publish(Exchange exchange){
> >                Publishable publishable=
> > exchange.getIn().getBody(Publishable.class);
> >                Map<String,String> origMsgHeaders =
> > publishable.getPublishProperties();
> >                Map<String,Object> msgHeaders = new HashMap<String,
> > Object>(origMsgHeaders);
> >                String destinationName = null;
> >
> >                //If we are publishing on muiltiple topics and there is
> > no destinationPrefix can't publish this msg
> >                if(publishType == PublishType.MULTIPLE_DESTINATIONS){
> >
> > if(!StringUtils.isEmpty(publishable.getPublishTopicName())){
> >                                destinationName = String.format("%s.%s",
> > destinationPrefix, publishable.getPublishTopicName());
> >                        }else{
> >                                log.error("Empty topicName for msg: " +
> > publishable);
> >                        }
> >                }
> >                else{
> >                        //The we are publishing on 1 topic, grab it from
> > config
> >                        destinationName = destinationPrefix;
> >                }
> >
> >                if(log.isDebugEnabled()){
> >                        log.debug("Destination is " + destinationName);
> >                }
> >                msgHeaders.put(JmsConstants.JMS_DESTINATION_NAME,
> > destinationName);
> >                exchange.getIn().setHeaders(msgHeaders);
> >        }
> >
> >
> >
> ===============================================================================
> > Please access the attached hyperlink for an important electronic
> > communications disclaimer:
> > http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> >
> >
> ===============================================================================
> >
> >
>
>
> ===============================================================================
> Please access the attached hyperlink for an important electronic
> communications disclaimer:
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
> ===============================================================================
>
>

RE: Unit testing a method with Exchange as a Param

Posted by "Gershaw, Geoffrey" <ge...@credit-suisse.com>.
Christian,

You are a gentleman and a scholar. I need to be a better Camel Rider. I didn't realize how easy it is to Unit test. I thought I'd have to launch the whole Spring dependency.

Thanks again

Geoffrey A. Gershaw
Credit eTrading Dev - RDO
1 919 994 6412


-----Original Message-----
From: Christian Müller [mailto:christian.mueller@gmail.com] 
Sent: Thursday, May 24, 2012 6:40 PM
To: users@camel.apache.org
Subject: Re: Unit testing a method with Exchange as a Param

I wouldn't mock the Exchange:

CamelContext ctx = new CamelContext();
Exchange ex = new DefaultExchange(ctx);

// your code...

Best,
Christian

On Thu, May 24, 2012 at 11:31 PM, Gershaw, Geoffrey <
geoffrey.gershaw@credit-suisse.com> wrote:

> Hello all,
>
> I am trying to unit test the following method, which is essentially the
> Sending to multiple JMS destinations from one endpoint example from the
> camel website.  Since the Exchange object is a param and contains the
> result, I'm having trouble Mocking it. The below test fails at the ***
> line. actualTopicName is null :( . Essentially all the publish method is
> doing, is adding headers to the In Msg of the Exchange object.
>
> Appreciate the advice.
>
> Geoff
>
>
>
>        @Test
>        public void testSingleTopicPublish(){
>                MarketData md = createMarketData();
>
>                Exchange exchange = mock(Exchange.class);
>                Message inMsg = mock(Message.class);
>
>
>                when(exchange.getIn()).thenReturn(inMsg);
>                when(inMsg.getBody(Publishable.class)).thenReturn(md);
>
>
>                String singleTopic = "TEST.TOPIC";
>                destinationResolver = new
> JMSDestinationResolver(singleTopic,PublishType.SINGLE_DESTINATION);
>                destinationResolver.publish(exchange);
>
>                Message actualInMsg = exchange.getIn();
>                String actualTopicName = (String)
> actualInMsg.getHeader(JmsConstants.JMS_DESTINATION_NAME);
>
>                assertEquals(singleTopic, actualTopicName);
> ************************************
>
>
>        }
>
>
>        /**
>         * Send exchange to a JMS Destination determined at Runtime
>         * @param exchange
>         */
>        public void publish(Exchange exchange){
>                Publishable publishable=
> exchange.getIn().getBody(Publishable.class);
>                Map<String,String> origMsgHeaders =
> publishable.getPublishProperties();
>                Map<String,Object> msgHeaders = new HashMap<String,
> Object>(origMsgHeaders);
>                String destinationName = null;
>
>                //If we are publishing on muiltiple topics and there is
> no destinationPrefix can't publish this msg
>                if(publishType == PublishType.MULTIPLE_DESTINATIONS){
>
> if(!StringUtils.isEmpty(publishable.getPublishTopicName())){
>                                destinationName = String.format("%s.%s",
> destinationPrefix, publishable.getPublishTopicName());
>                        }else{
>                                log.error("Empty topicName for msg: " +
> publishable);
>                        }
>                }
>                else{
>                        //The we are publishing on 1 topic, grab it from
> config
>                        destinationName = destinationPrefix;
>                }
>
>                if(log.isDebugEnabled()){
>                        log.debug("Destination is " + destinationName);
>                }
>                msgHeaders.put(JmsConstants.JMS_DESTINATION_NAME,
> destinationName);
>                exchange.getIn().setHeaders(msgHeaders);
>        }
>
>
> ===============================================================================
> Please access the attached hyperlink for an important electronic
> communications disclaimer:
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
> ===============================================================================
>
>

=============================================================================== 
Please access the attached hyperlink for an important electronic communications disclaimer: 
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html 
=============================================================================== 


Re: Unit testing a method with Exchange as a Param

Posted by Christian Müller <ch...@gmail.com>.
I wouldn't mock the Exchange:

CamelContext ctx = new CamelContext();
Exchange ex = new DefaultExchange(ctx);

// your code...

Best,
Christian

On Thu, May 24, 2012 at 11:31 PM, Gershaw, Geoffrey <
geoffrey.gershaw@credit-suisse.com> wrote:

> Hello all,
>
> I am trying to unit test the following method, which is essentially the
> Sending to multiple JMS destinations from one endpoint example from the
> camel website.  Since the Exchange object is a param and contains the
> result, I'm having trouble Mocking it. The below test fails at the ***
> line. actualTopicName is null :( . Essentially all the publish method is
> doing, is adding headers to the In Msg of the Exchange object.
>
> Appreciate the advice.
>
> Geoff
>
>
>
>        @Test
>        public void testSingleTopicPublish(){
>                MarketData md = createMarketData();
>
>                Exchange exchange = mock(Exchange.class);
>                Message inMsg = mock(Message.class);
>
>
>                when(exchange.getIn()).thenReturn(inMsg);
>                when(inMsg.getBody(Publishable.class)).thenReturn(md);
>
>
>                String singleTopic = "TEST.TOPIC";
>                destinationResolver = new
> JMSDestinationResolver(singleTopic,PublishType.SINGLE_DESTINATION);
>                destinationResolver.publish(exchange);
>
>                Message actualInMsg = exchange.getIn();
>                String actualTopicName = (String)
> actualInMsg.getHeader(JmsConstants.JMS_DESTINATION_NAME);
>
>                assertEquals(singleTopic, actualTopicName);
> ************************************
>
>
>        }
>
>
>        /**
>         * Send exchange to a JMS Destination determined at Runtime
>         * @param exchange
>         */
>        public void publish(Exchange exchange){
>                Publishable publishable=
> exchange.getIn().getBody(Publishable.class);
>                Map<String,String> origMsgHeaders =
> publishable.getPublishProperties();
>                Map<String,Object> msgHeaders = new HashMap<String,
> Object>(origMsgHeaders);
>                String destinationName = null;
>
>                //If we are publishing on muiltiple topics and there is
> no destinationPrefix can't publish this msg
>                if(publishType == PublishType.MULTIPLE_DESTINATIONS){
>
> if(!StringUtils.isEmpty(publishable.getPublishTopicName())){
>                                destinationName = String.format("%s.%s",
> destinationPrefix, publishable.getPublishTopicName());
>                        }else{
>                                log.error("Empty topicName for msg: " +
> publishable);
>                        }
>                }
>                else{
>                        //The we are publishing on 1 topic, grab it from
> config
>                        destinationName = destinationPrefix;
>                }
>
>                if(log.isDebugEnabled()){
>                        log.debug("Destination is " + destinationName);
>                }
>                msgHeaders.put(JmsConstants.JMS_DESTINATION_NAME,
> destinationName);
>                exchange.getIn().setHeaders(msgHeaders);
>        }
>
>
> ===============================================================================
> Please access the attached hyperlink for an important electronic
> communications disclaimer:
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
> ===============================================================================
>
>