I am currently building a component for sending and receiving messages to different JMS brokers (eg ActiveMQ, TibcoEMS). Obviously, I would like to build unit tests in order to verify the functionality of this component. One thing that I would like to test would be a method for sending a message.
public void Send( IJmsMessage jmsMessageToSend ) {
TIBCO.EMS.Connection connection = TempPermLayer.GetConnection();
connection.Start();
Session session = TempPermLayer.GetSession(connection);
MessageProducer producer = TempPermLayer.GetMessageProducer(session);
try {
IMessageFactory<Message> messageFactory = new TibcoEmsMessageFactory(session, producer);
Message message = messageFactory.ProduceMessageUsing(jmsMessageToSend);
producer.Send(message);
}
finally {
TempPermLayer.CleanUp();
}
}
The layer classes are there to give fine grained control over how the method actually behaves and as such are necessary. In the end the method above would boil down to something like this:
public void Send( IJmsMessage jmsMessageToSend ) {
ConnectionFactory connectionFactory = new ConnectionFactory(Host);
Connection connection = connectionFactory.CreateConnection();
connection.Start();
Session session = connection.CreateSession(false, SessionMode.ClientAcknowledge);
Destination dest = session.CreateQueue("myQueue");
MessageProducer producer = session.CreateProducer(dest);
IMessageFactory<Message> messageFactory = new TibcoEmsMessageFactory(session, producer);
Message message = messageFactory.ProduceMessageUsing(jmsMessageToSend);
producer.Send(message);
}
IJmsMessage
and TibcoEmsMessageFactory
are basically wrappers for broker specific implementations (eg TIBCO.EMS.Message
).
Testing the first provided method would not actually be testing functionality I do not need to test, like how the Tibco EMS Client implementation creates a connection. There is quite some additional functionality involved - unlike in the second method, which is something I would not actually test ;)
I see two approaches for testing such a method:
The actual question(s):
Are there any other approaches or possibilities I am forgetting? If not, which method of the above would you think is preferable in my case?
Mock a JMS implementation sounds tedious. Consider this article if you still want to do it.
I would personnaly prefer having and automating a local JMS broker... How about using a lightweight server such as FFMQ ?
Both ideas I took in another post .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.