c# - How to mock MQ-Series object MQQueueManager with MOQ for .NET? -
i trying unit test mq-series implementation of project, , need mock connection mq-series server test implementation isolated , avoid sending messages/retrieving messages actual queue.
here code:
using ibm.wmq; using microsoft.visualstudio.testtools.unittesting; using moq; namespace test { [testclass] public class mqtest { [testmethod] public void sendmessage_moqunittest() { //create mock mq manager var mqmanmock = new mock<mqqueuemanager>(); //test calling send method mymqhandler.mqsender mqsender = new mymqhandler.mqsender(); //error happens when trying access moq object here mqsender.send("test message", mqmanmock.object); } } }
i aware not doing setup on moq yet, problem appears before code run against it.
the problem when exception. stack trace inner exception message "i/o error occurred":
at ibm.wmq.mqchanneltable.createchannelentrylists(mqchannellistentry namelist) @ ibm.wmq.nmqi.managednmqimq.createnamelistentryfromccdt(string qmgrname, string ccdtfile) @ ibm.wmq.nmqi.managednmqimq.createnamelistentryfromccdt(string qmgrname) @ ibm.wmq.nmqi.managednmqimq.getnamelist(string qmgrname) @ ibm.wmq.nmqi.managednmqimq.doconn(string qmgrname, mqconnectoptions cno, managedhconn manhconn, int32& hconn, int32& compcode, int32& reason) @ ibm.wmq.nmqi.managednmqimq.mqconnx(string pqmgrname, mqconnectoptions pconnectopts, phconn phconn, int32& pcompcode, int32& preason) @ ibm.wmq.mqqueuemanager.connect(string queuemanagername) @ ibm.wmq.mqqueuemanager..ctor() @ castle.proxies.mqqueuemanagerproxy..ctor(iinterceptor[] )
most of time run though "function evaluation timed out" og mqmanmock.object.
does mean can't mock mq??
looks hitting mqqueuemanager.
what need put mqqueuemanager behind own interface (assuming class doesn't implement interface) , use mock build interface.
so create imqqueuemanager send method , in implementation work mqqueuemanager consuming classes should have interface passed them through constructor in order use mqqueuemanager.
so write implementation this
public interface imqqueuemanager { void send(string message, mqmanmock obj); } public interface concretemqqueuemanager : imqqueuemanager { public void send(string message, mqmanmock obj) { //create mock mq manager var mqmanmock = new mqqueuemanager(); //test calling send method mymqhandler.mqsender mqsender = new mymqhandler.mqsender(); //error happens when trying access moq object here mqsender.send("test message", mqmanmock.object); } }
then writing unit tests against class below
public class myclass { imqqueuemanager _manager public myclass(imqqueuemanager queuemanager) { _manager = queuemanager; } public void additemtoqueue(string myitem) { _manager.send("hello",mqmanmock.object); } }
and using moq verify send called
Comments
Post a Comment