Getting started: automated tests


Everyone knows that auto­mated tests are use­ful. When you are just start­ing a new pro­ject, it is easy to include all sorts of auto­mated test­ing dir­ectly from the start. You can take all sorts of things into account: auto­matic unit tests, auto­matic user inter­face tests, or even con­tinu­ous integ­ra­tion with auto­matic tests.

But what if you have an exist­ing code base that you wish to add auto­matic test­ing to? Chances are that you can­not just do extens­ive changes to your cur­rent code base, just to add auto­mated tests. Where to start? My advice: start small.

This art­icle will show you how to add auto­mated unit tests to your jar builds. This is fairly simple, and can be done without mov­ing your entire code base around.

Automated unit tests

The first step is to a fully auto­mated test suite is to add auto­matic unit tests to your jar builds. This is a small step that can eas­ily be done even with an exist­ing code base. You will need Ant and JUnit.

First, add a new source folder to your pro­ject. I usu­ally call this src-test. This folder stores all test classes. The advant­age of a sep­ar­ate folder is that the tests do not clut­ter your code base, while you can still use the same pack­age as the class you are test­ing. Having the test class have the same pack­age as the class you are test­ing is a huge advant­age because it allows for easy access to the class under test.

The buildfile

The second step is to alter your Ant build­file to auto­mat­ic­ally run the tests once the jar is built. When one of the tests fails, build­ing the jar will fail. This way, you will never have untested code in your jar.

We start with the fol­low­ing simple build­file. This file simply builds a jar from com­piled classes that exist in the bin folder.

We need to make three addi­tions to the build­file: we need to tell Ant the class path for the tests, we need to tell Ant where it can find the tests and we need to instruct Ant to run these tests using JUnit.

We begin by adding the instruc­tions for Ant to run the tests. Along the way, we will find out what other inform­a­tion Ant needs. We can add this to the build­file once it is needed.

These instruc­tions come in the form of a new tar­get, shown below. 

I believe this piece of XML is mainly self-explanatory. This is what it does:

Line 2: We instruct JUnit to halt on fail­ure and on error. This ensures that the build will fail once a test fails.

Line 4: We add a batch test. Line4 adds the file­set. The source folder has been put in a vari­able, which we will need to intro­duce. In the batch tests, we include all files that have a file­name that ends in, for example and

Line 8: We add a format­ter. I will not talk about format­ters in detail in this art­icle. More inform­a­tion about format­ters can be found on the Junit task page for Ant.

Line 9: We provide the classpath to use. This classpath needs to con­tain junit.jar and ant-junit.jar. We will spe­cify the exact path next. 

The classpath

To spe­cify the classpath, we need a path tag. We can use this to build the classpath.

The id of the classpath is the same as the refid attrib­ute we saw earlier. We add all classes in the bin folder to the class path, as wel as all jar files in the lib folder.

Wrapping up

We still need to do two things. We need to tell Ant that test­ing is neces­sary for build­ing a jar. We can do this by adding a depends attrib­ute to the tar­get that builds the jar. In this attrib­ute, we can add other tar­gets that need to fin­ish suc­cess­fully before this tar­get can start.

The second thing we need to do is add the vari­ables we have used through­out this art­icle. Using vari­ables makes it easy to change cer­tain para­met­ers of your build file later, for example the loc­a­tion of folders.

The final build­file looks like this.

And we are done! We have added unit tests to the build pro­cess of jars. This is a simple pro­ced­ure that you can use to quickly start using unit tests with an exist­ing code base.

Further reading

Ant doc­u­ment­a­tion on the JUnit task