views:

198

answers:

3

I have developed a basic Chat application in Java. It consists of a server and multiple client. The server continually monitors for incoming messages and broadcasts them to all the clients. The client is made up of a Swing GUI with a text area (for messages sent by the server and other clients), a text field (to send Text messages) and a button (SEND). The client also continually monitors for incoming messages from other clients (via the Server). This is achieved with Threads and Event Listeners and the application works as expected.

But, how do I go about unit testing my chat application? As the methods involve establishing a connection with the server and sending/receiving messages from the server, I am not sure if these methods should be unit tested. As per my understanding, Unit Testing shouldn't be done for tasks like connecting to a database or network.

The few test cases that I could come up with are:
1) The max limit of the text field
2) Client can connect to the Server
3) Server can connect to the Client
4) Client can send message
5) Client can receive message
6) Server can send message
7) Server can receive message
8) Server can accept connections from multiple clients

But, since most of the above methods involve some kind of network communication, I cannot perform unit testing. How should I go about unit testing my chat application?

+3  A: 

You should test the server and client in isolation.

The way to do this is to use mock objects to mock either the server (for testing the client) or the client (for testing the server).

A mock server would have the same methods as the real server, but you can decide what they return, i.e. simulate a connection error, a timeout, etc. Because it is a mock, you have full control over the functioning and you don't have to worry about actual connection errors.

For Java, look at the Mockito mocking framework.

Frederik
+2  A: 

Unit tests should be focused on exercising public APIs of each class you have built. However, things get a little tricky when dealing with Swing. Consider swingUnit for unit testing Swing components.

ring bearer
+2  A: 

Chapter 7 of Beautiful Testing describes testing an XMPP chat client. I recommend reading the chapter. The conclusion is illustrative and may provide some pointers for your chat application:

In our quest to create beautiful tests for checking XMPP protocol implementations, we started out by testing simple request-response protocols at the lowest level: the data sent of the network stream. After discovering that this form of testing does not really scale well, we abstracted out the protocol to a higher level, up to the point where the tests used only high-level data structures. By testing protocol behavior on a high level, we were able to write tests for more complex protocols without compromising the clarity of the tests. For the most complex protocols, writing scenarios helped to cover all of the possible situations that can arise in a protocol session. Finally, since XMPP is an open protocol with many different implementations, it's very important to test an XMPP application on the real network, to ensure interoperability with other implementations. By running small test programs regularly, we were able to test the system in its entirety, and check whether our implementation of the protocol plays together nicely with other entities on the network.

Brett Daniel