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.gers...@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
>
> ===============================================================================
>
>

Reply via email to