Preparing for Test-First Development

Problem

You want to configure your development environment to support test-first development with HttpUnit, JUnit, Tomcat, and Ant.

Solution

Create an Ant buildfile to automatically build and deploy your web application. The buildfile allows you to quickly redeploy and test after each change to your code.

Discussion

The example shown in this recipe relies on Tomcat 4.0 or later, as well as Ant Version 1.5. It uses Tomcat’s manager application to deploy and redeploy the web application while Tomcat is running. The ability to redeploy a modified web application while the server is running is critical for test-first development because it takes too long to restart most servers. A successful XP approach depends on your ability to make lots of small code changes quickly.

In order to test using Ant’s junit task, you should copy servlet.jar, httpunit.jar, junit.jar, and Tidy.jar to Ant’s lib directory. This makes it easy to ensure that all of the required JAR files are loaded using the same Java ClassLoader when you are running your tests. Ant class loading issues were discussed in Recipe 3.15.

The first part of your buildfile should define a classpath:

<path id="classpath.project">
  <pathelement path="${dir.build}"/>
</path>

This path picks up all class files from your build directory. Ant also includes all of the JAR files from the ANT_HOME/lib directory. This allows you to compile the code with this target:

<target name="compile" depends="prepare" description="Compile ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.