Chapter 5. Testing Builds with JUnit

This chapter is about a crucial aspect of the build processtesting build results before deploying them. It doesn't make sense to deploy a build that has been broken, and using the JUnit framework with Ant, you can run tests on your code and deploy a build only if it satisfies those tests. This is a great way to make sure changes to your code haven't broken anything.

To test the results of a build automatically, you'll need to use one of Ant's most powerful optional tasks: junit. This task is part of the repertoire of every serious Ant developer, especially those working in teams. If someone else on your project has broken your code, you should know about it before you deploy or upload to a shared code repository, and junit will let you know about these problems automatically.

To demonstrate how JUnit works with Ant in this chapter, we're going to use Project.java, shown in Example 5-1, as a guinea pig.

Example 5-1. A simple Project file

package org.antbook;

public class Project 
{
    public Project (String name) 
    {

    }
    
    public boolean returnTrue( ) 
    {
        return true;
    }

    public int return4( ) 
    {
        return 2 + 2;
    }

    public Object returnObject( ) 
    {
        return new Integer(1);
    }

    public static void main(String args[]) 
    {
        Project project = new Project("project");
        System.out.println(project.returnTrue( ));
        System.out.println(project.return4( ));
        System.out.println(project.returnObject( ));
    }
}

This application, Project.java, has three simple methods, each of which returns a value: ...

Get Ant: The Definitive Guide, 2nd Edition 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.