Making FitNesse Maven friendly (now with Slim)

Update Thanks to Alan Palmer for this trick that makes this whole post unnecessary: If you use
!path {java.class.path}
You get the classpath that fitnesse was called with, without the need for changing the fitnesse code.

Regular readers of my blog may remember that I’ve researched how to get the classpath from the FitNesse-process inherited by the Fit process that FitNesse spawns when it runs a test. This trick is an easy way to get around having to specify classpath variables in your FitNesse tests. This blogposts provides an easier way, plus compatibility with the new Slim testrunner in FitNesse.

This is the basic process:

  1. Create a Maven project with a dependency on your System Under Test in addition to FitNesse.
  2. Insert a few bootstrap files and classes into the project (to be explained)
  3. Start fitnesse.FitNesse as a main class in the project
  4. All classes in the project and its dependencies will now be available in Fixture classes

This article uses the 20081115 version of FitNesse, which is not yet in the Maven repository. To get it yourself, download it from the FitNesse website.

The problem

Fixing the classpath is exceedingly simple. But you need to understand a bit of the inner workings of FitNesse to get it to work. When you press the “test” button on a test, or the suite button on a test suite, FitNesse instantiates a class based on the registered “responder”. Our first order of business is to override this, but in order to do that, we need to take control over FitNesse.

You can do this by adding fitnesse.jar and fitlibrary.jar to your classpath, either manually in Eclipse, or by using a Maven dependency on org.fitnesse:fitnesse and org.fitnesse:fitlibrary.

Once FitNesse is in you IDE’s classpath, you can run the Java class “fitnesse.FitNesse”. This starts FitNesse on port 80. Navigate to, say “http://localhost/MyFirstTest”, and you’re ready to add a test.

In order to get it to work, though, you will have to do something like the following:

Ugh! Bad FitNesse!

The solution

What actually happens when you press “test” is that FitNesse creates and executes a Responder class. You can override a responder by adding a file called plugins.properties to the current working directory. Here is an example of such a file:

As you can see, I replace the test-responder with my own subclass. Here is the code of the InheritClasspathTestResponder for the very latest version of FitNesse:

For older versions of FitNesse, you can instead override the buildCommand method as follows:

This works both with both the original Fit test runner and the new Slim test runner. To try it out with a Slim runner, insert the following into a test page:

(Notice that Uncle Bob’s article introducting Slim mistakenly !defines TEST_RUNNER. The correct variable is TEST_SYSTEM)

Debugging

In my original experiment, I also managed to get debugging support for FitNesse. This is how it works:

  1. After running a test, replace the part ?test in the URL with ?debug. The page will look like it’s hanging while the server is waiting for the debugger.
  2. In your IDE, attach a remote debugger to the Fit-process.
  3. You can now debug your code just as normal

This turned out to be a lot harder with the newest version of FitNesse. To support Slim, this version introduces the concept of TestSystem classes, of which there are two implementations, FitTestSystem and SlimTestSystem. These classes build up the (Java) command line to execute the test runner.

As of the current version of FitNesse, there is no plugin point to put new implementations of TestSystem, so I had to change TestResponder implementation of performExecution quite a bit. The original has some caching and other freaky stuff which I finally gave up trying to override. Sadly, my implementation only works for Slim. For some reason, overriding FitTestSystem#buildCommand causes FitNesse to hang. So: Use at your own risk.

To install the code, you have to modify the plugins.properties file described above:

Here is the awful implementation of DebugTestResponder. I’ll see if I can get changes implemented in FitNesse to make it easier.

In conclusion

It is possibly, you could even say easy, to make Maven and FitNesse play nice. The trick with InheritClasspathTestResponder shows how you can stop maintaining the FitNesse classpath separate from the Maven classpath, which makes FitNesse into just another main-class in your system.

Sadly, the structure around the new TestSystem classes needs a little improvement to support debugging.

About Johannes Brodwall

Johannes is Principal Software Engineer in SopraSteria. In his spare time he likes to coach teams and developers on better coding, collaboration, planning and product understanding.
This entry was posted in English. Bookmark the permalink.
  • Daniel Carleton

    Thanks for writing this post.  Saved me a lot of pain!  My use case is running FitNesse directly inside the IDE, which of course I’d like to have manage the classpath.

  • Makarand Adnaik

    I think this works with old version, do you have anything which we can try with latest version, instead of adding dependency as a whole in the test we are looking for something we can refer as common repository. Please help.
    –Makarand

  • Thanks for the question, Makarand. This was actually answered in a comment on another post on the subject: “If you use !path {java.class.path} You get the classpath that fitnesse was called with, without the need for changing the fitnesse code.” Hope this helps.