Unit tests are of course really useful to make sure your code works and continues to work. I use JUnit for writing unit tests, but if you throw them into your ‘src’ directory, they get bundled into the penlet itself! Not very desirable.
Having a quick look at the ant script invoked to deploy a penlet, it would be possible to extend this script. But the files are supplied by LiveScribe (and may in a future release be updated by LiveScribe again), so I decided it was better to not touch them.
Instead, I use settings in Eclipse to tell Eclipse about the test classes (put in a separate directory) without the ant script knowing about them. That way I can at least run the unit tests in Eclipse without the test cases sneaking into the deployed penlet.
Testing LiveScribe classes… Not!
I don’t have a solution at present for classes that use LiveScribe pen classes (like StrokeStorage) – there is an emulator coming which *might* provide useful stub classes here, but I am not sure. It’s also tricky if you are using the ICRContext for handwriting recognition (or other similar complex functionality). My best solution to date is to separate application logic from LiveScribe classes as much as possible, making it at least possible to test your application logic with unit tests.
If you create a new project (using the LiveScribe supplied wizard) you will have a ‘src’ directory. The following steps describe one way to add unit test classes to the project without them ending up in the deployed penlet.
- Right click on the project and select ‘New’ –> ‘Folder’. I called the new folder ‘test-src’.
- Right click on the project and select ‘Properties’ (right down the bottom). Select ‘Java Build Path’, then on the ‘Source’ tab, click ‘Add Folder…’. Select the test-src directory you created above.
- Tick the checkbox “Allow output folders for source folders” at the bottom of the Java Build Path page.
- Click on the ‘Output Folder’ setting of the test-src directory (on the ‘Source’ tab of the ‘Java Build Path’ window). Enter a directory such as ‘test-classes’ for where compiled class files can go (rather than into the default location). (I am not sure this step is strictly necessary, but I do it anyway to be sure.)
- Go to the ‘Libraries’ tab of the ‘Java Build Path’ window and make sure JUnit3 is listed. (Add it if not there.)
- On the ‘Export and Order’ tab, make sure JUnit3 and Penlet Libraries are not ticked (so they are not exported). (I think this is the default set up by the wizard.)
- Put your unit test classes under the test-src directory. I personally for a class such as Chord would call the test class ChordTest (rather than TestChord) as it groups the Chord and ChordTest classes together better. (This is however completely personal.)
- Right click and select ‘Run As…’ –> ‘JUnit Test’. The test should run (and hopefully pass!). Deploying a penet will not include the test classes.
Unit tests are great to make sure your non-LiveScribe dependent application logic is working. Unit tests also help make sure that over time if you make changes you have some confidence you have not broken the code. I don’t have a solution for testing code dependent on LiveScribe provided penlet functionality (such as asking for strokes). I am curious to see if the emulator coming out Real Soon Now will help with testing or not. We shall see. Until then, separate your application code an LiveScribe code and unit test the application code.