junit - JMS Testing - JMSTemplate send not executed -


i have code piece sending jms messages via spring jmstemplate. testing the method use mockito.

my code looks following.... publishdialogueservicemessage()->

 brokerurl = jmsqueueproperties.getproperty(messagerouterconstants.jms_queue_url);           log.info("the broker url : {}", brokerurl);           jmstemplate.send(jmsqueueproperties.getproperty(messagerouterconstants.queue), new messagecreator() {              @override             public message createmessage(session session) throws jmsexception {                              objectmessage obj = session.createobjectmessage(serviceresponse);                 messagesent = true;                 return obj;             }         }); 

in above code set boolean variable true, check if message sent

my test looks following,

@before     public void setup() throws exception {          connectionfactory = mockito.spy(new activemqconnectionfactory(                 "vm://localhost?broker.persistent=false"));         conn = connectionfactory.createconnection();         conn.start();            }   @after public void cleanup() throws exception{     conn.stop(); } 


@test     public void testpublishdialogueservicemessage()     {         serviceresponse response = mockito.mock(                 serviceresponse.class, mockito.withsettings()                         .serializable());         jmstemplate mocktemplate = mockito.mock(jmstemplate.class);         java.util.properties p = mockito.mock(java.util.properties.class);               mockito.when(p.getproperty(messagerouterconstants.queue))                 .thenreturn("outbound.request.queue");         mocktemplate.setconnectionfactory(connectionfactory);         mocktemplate.setdeliverypersistent(true);         mocktemplate.setsessionacknowledgemode(2);         mocktemplate.setsessiontransacted(true);          reflectiontestutils.setfield(publisher, "jmsqueueproperties", p);         reflectiontestutils.setfield(publisher, "jmstemplate", mocktemplate);          // test         publisher.publishdialogueservicemessage(response);         argumentcaptor<messagecreator> msgcreator = argumentcaptor.forclass(messagecreator.class);         mockito.verify(p, mockito.times(2))                 .getproperty(mockito.anystring());         mockito.verify(mocktemplate, mockito.times(1)).send(                 mockito.anystring(), mockito.any(messagecreator.class));          //messagecreator msgcrt = mockito.spy(msgcreator.getvalue());         //assert.notnull(msgcrt);          assert.istrue(publisher.ismessagesent());     } 

in test facing interesting problem publisher.ismessagesent() returns me false indicating send message seems not executed(?). mockito.verify(mocktemplate, mockito.times(1)).send(mockito.anystring(), mockito.any(messagecreator.class)); goes fine.

i wondering cause messagesent variable not setting. can shed light might doing wrong.

simple, have mock jmstemplate (your mocktemplate). when method invoked on mock doesn't other record call mock. mock doesn't know should attempt invoke msgcreator.

looking @ test see obvious issues suggest lack of knowledge of mockito. why setting of fields on mocktemplate? mock, not use fields anyway. suggests don't need code in @before , @after.

if want test send message via jms (and thereby invoke message createor) should use spy on jmstemplate instead of mock. however, highly discourage test dependent on external system , in effect testing jsmtemplate. fact mock gets invoked sufficient. additional thing think need invoke message creator being passed mock verify creates message correctly.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -