mercredi 25 février 2015

Dealing with JMS broker dependencies in unit tests

I am currently building a component for sending and receiving messages to different JMS brokers (e.g. 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 (e.g. 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:



  1. Mock all the dependencies, down to the core. This is the approach I am currently taking, and it proves to be very labor intensive - which is about the only real drawback I can see so far.

  2. Host an actual JMS broker somewhere, probably locally. This might be as simple as a jar-file that is executed at the beginning of a unit test. This would make unit tests easier to write initially, I believe. I have seen this approach being used in some Rest-clients (just launch Jetty for example). It relieves the need to mock all the dependencies, but it could make the unit-test process harder to maintain in the long run - especially when a build server is involved.


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?


Aucun commentaire:

Enregistrer un commentaire