Smartfox Extension Unit Testing (the beginning)
This has been an ongoing interest of mine ever since I started developing server side Java extensions for Smartfox. As part of a responsible development methodology, Unit Testing is a very important facet to ensuring a product that meets strict quality assurance standards. Combine this with some sort of continuous integration that will perform these tests for you and you have the makings of a well put together development cycle that includes development, testing, and deployment. As many who read this will no doubt be aware, unit testing of an extension developed for the Smartfox Server can prove to be a difficult task. This is mostly due to the decoupled asynchronous (event based) communication structure that is designed to allow dialogue between your client application and the Smartfox Server. Anyone familiar with nUnit Unit Testing frameworks will realize that they are built to perform synchronous tasks with a strict beginning (setUp) and end (tearDown). This presents an issue because of the previously mentioned problem of asynchronous communication. We can, however, begin to move closer to a good solution of testing by “faking” an asynchronous call to the server and have a convenient runner attached for easy firing of multiple testing facilities. The question of Unit Testing extensions has been covered several times in the Smartfox Server forums with a very good suggestion by Lapo to decouple your extensions in such a way so as to perform localized testing procedures on the specific methods that you will use directly in an extension (that is to say that you use some sort of decoupling pattern such as DAO). However, I have personally found myself in situations many times that require me to develop for a client (Flash / AS3) application that I will not be able to test against because, frankly, the code for that piece has not been written yet. I would like to have a formalized way to look at and / or validate against communication that is prompted by my extension. If I can do this in a repeatable “automatic” way, all the better. I have found that by extending the JUnit framework, this is possible. Keep in mind that you will be validating input against whatever protocol you have chosen for your exchange between client and server. At present, I believe this includes raw (with a delimiter), xml, and json. This means that you will need to develop the logic necessary to verify that your expected input is correct. This will be one of the improvements that I would like to make to this project, a library that adheres to the protocol specification for communication in xml and json. At the very least, this will lend a formalized way to view debug communication across the network. This is probably a good place to let you know that you will want to be fairly experienced with the JUnit testing framework in order to implement this solution as it is beyond the scope of this article to explain how to set it up. In addition there are many articles that are more than adequate to explain this. I will say, however, that I enjoy the integration of JUnit with Eclipse and it lowers the barrier to entry quite low.
I will share the code that I have written thus far at the end of this post, but I must stress that this is a rough draft, if you will, that I intend on improving over time and would like to get feedback on. Consider this the first foray into a community effort to bringing repeatable reliable Unit Testing to Smartfox extensions.
From looking at the code, you will see that it is rather simplistic in nature. This is what I like about it. If Unit Testing requires too much work (read, more work than developing the application that you are testing against) then it will lead the programmer to most likely eschew testing for more important tasks, not the least of which shipping code on time (and on budget).
As a brief overview, in no uncertain terms, I extended the base TestCase class and simultaneously implemented the ISFSEventListener interface to facilitate the listening for events that are fired as a result of many of the basic tasks required to connect to Smartfox. I have relied primarily on the Java client API to achieve all of the client methods required to make this testing possible. If you will notice, my effort to “fake” an asynchronous challenge / response with Smartfox is achieved through a sleep state during specific steps of the JUnit testing lifecycle. Also, please notice that another sleep is required after a call to an extension in each of your testing methods. I realize that this could be handled in a much more elegant manner through the use of a Timer (perhaps) and this is first on my list to improve. The project that I am currently working on has very little latitude with respect to time allowed for tasks such as this, so I am forced to develop a lot of this during my “free time”. If there are other improvements or requests for a feature let me know in the comments or at the forum. If this gains any amount of popularity, I will host it on Google code to formalize its development.
With that I will leave the code for you here to download. Enjoy and feel free to talk back.


I’m not sure if this has been around for a bit or if it is, in face, a new feature. However, I think this is the most compelling feature since the redesign of google reader.