25 August 2007

jUnit, testing, testing

I finally wrote and succesfully run my first jUnit test :) Yay! (in fact that was a couple of days ago, but who counts?) In a way it was easier than I expected, but in some other ways, getting it to run and give the proper results was quite a tedious work.
It was easier than I expected, because all my previous contact with unit testing was a couple of years back, when I attended a short presentation about TDD with Visual Studio 2005 Beta2. And at the time everything was quite fuzzy for me. But the official documentation and the actual jUnit package for the dummy Gibberish protocol in SIP-Communicator proved to be quite a source of excelent samples :) .
Getting it run, and with the proper results was, though a totally different chore, mainly because of all the OSGi stuff inside SIP-Communicator (which, I must admit, I still don't fully understand). It took me a couple of hours and some help from Emil to edit the 3 (or was it 4) configuration files needed for the tests to be properly run. All in all it wasn't big thing, but getting it right from the first try might be tricky. That's way, and because it was mainly a trial-and-error process, I'll make a quick outline below:
  1. make sure your package mainfest file is correct (for the RSS this was rss.provider.slick.manifest.mf ). Care should be taken about which packages are imported using the Import-Package directive. No java.* (that includes packages like java.util or java.net) package should be included or Felix will throw an exception and won't load your bundle. This alone gave me a headache :D
  2. Edit lib/felix.unit.test.properties in order both to include aditional packages you might use and also to include your slick bundle (in this case file:sc-bundles/protocol-rss-slick.jar) in the list of bundles started by the ant build test. Again, include only packages not provided by the runtime. One important thing to note here is that you must also include the bundle that provides your protocol implementation (in this case file:sc-bundles/protocol-rss.jar). Even if it may be outright obvious, I had some rough time figuring out why it couldn't find any protocol provider for the RSS implementation.
  3. Create an Ant build target for your testing bundle and include it in the list of run targets.
  4. If you want to run just your tests, you might edit lib/testing.properties and comment the lines corresponding to the other tests and add only your package.
If you made the above steps right you should have no problems running your tests and with the right outcome.
Happy testing! :)

No comments: