Sunday, June 6, 2010

A two week diversion. Eclipse Plugin to Generate Unit Tests

I took a new job about a month ago at an insurance company. The company is in the middle of rewriting the Point of Sale application for its line of business. Since the project had already kicked off and development had begun, I had been assigned the glorious task of filling in Unit test coverage.

You see, the management at this company is pretty new to Agile development methodologies, and until about a year ago, Unit tests were largely unknown to them. They are driving a tight timeline for delivering the rewritten project. Often, the developers are going over their burndown hours, so the first thing to suffer was, you guessed it, the unit tests. On top of that, management is insistent on getting 80% coverage. As you can probably guess from the title of this post, I was assigned the duty of creating unit tests for this iteration of the code, with the purpose of increasing code coverage to as close to 80% as possible.

When I was handed the code, the coverage, depending on sub-project, was between 0% and 20% covered.

I decided to create an Eclipse plugin to help me with the task (On my own time, of course.  During the day, I wrote tests.) I call it Unit Test Generation Plugin.  This version is very basic.  It really only works to create tests for POJOs at this point, but I think I'll extend it a bit to make it more useful soon.

I was really quite surprised at how easy and fun it was to create a plugin for Eclipse.   I thought I'd post some of the code and the process I used to create the plugin on the site.

Here are some highlights.  Let me know if you'd like me to write more detail about a particular topic.

Binding An Event To The Eclipse Package Menu
Eclipse uses a powerful plugin framework to allow you to extend and enhance the platform.  The way to add extensions is to use the PDE, which is a part of the core project.  Eclipse makes creating a new project VERY easy.  To start, go to File->New->Project...->Plugin Project.

In the New Plugin Window, Enter a name, like com.a.plugin.  The rest of the defaults can be left alone.  Click, Next.  Another popup appears to allow you to select more options for your project config.  Modify the name and provider all you like, but the defaults should suffice.  Click Next.

The final panel allows you to select several templates that demonstrate different extensions.  You can select a template if you like, or none at all.  For this project, I will select none.

When you finish, a new Eclipse plugin project is created and you are ready to continue.

Extension Points
An extension point is how you add new functionality to Eclipse.  They are configured using an XML file.  Eclipse supplies an editor to allow you to easily edit this file.  I had some trouble configuring my Handler through this screen, but was able to write the code by hand.  Once I edited it, it appeared correctly in the editor.  I've done a little reading and that might be because I didn't have some RCP plugins installed, but I haven't verified it yet.

Java Code Support
It also turns out that Eclipse has robust support for Java project Elements.  There are interfaces like ICompilationUnit, IPackageFragment and so forth.  It was pretty easy to generate new tests.

The Result
I have decided that I am going to make the project open-source and created a SourceForge project for it.  It is still in Alpha stage, and there is a bit of work to make it a 'real' plugin, but its now out there in the world.

Unit Test Generation Plugin

Now, I'm sure that Robert Martin would hunt me down if he knew what I created, and would destroy the laptop it was written on (OK, I don't really know him, but I've read one of his books, Agile Software Development, Principles, Patterns, and Practices.  From the perspectives presented there, I can infer that he would not be happy with my tool