Java Development with Ant
The most widely used build tool for Java projects, Ant is cross-platform, extensible, simple, and fast. It scales from small personal projects to large, multi-team J2EE projects. And, most important, it's easy to learn.
Java Development with Ant systematically explores what Ant can do, and how to apply it to your project. Whether you are new to Ant, or an experienced user, this book will show you powerful and creative uses for Ant. The book emphasizes basic concepts you need to know to effectively use Ant starting with Ant's XML-driven build process. It leads you step-by-step through everything you need to know to compile, test, package and deploy an application. It then guides you through the maze of more complex situations common in larger projects such as enterprise Java applications and Web Services. With this book you will gain access to a powerful tool to automatically build, test and deploy your Java software, no matter how simple or complex it might be.
CHAPTER 4: Testing with JUnit
"Any program feature without an automated test simply doesn't exist." 1
Software bugs have enormous costs: time, money, frustrations, and even lives. How
do we alleviate as much of these pains as possible? Creating and continuously executing
test cases for our software is a practical and common approach to address software
bugs before they make it past our local development environment.
The JUnit testing framework is now the de facto standard unit testing API for Java
development. Ant integrates with JUnit to allow executing test suites as part of the
build process, capturing their output, and generating rich color enhanced reports. In
this chapter, we cover in more detail what testing can give us beyond knowing that
our code is working well within some boundaries, then we cover the primary alternative
to JUnit testing and why it is insufficient. The bulk remainder of the chapter, the
largest part, is devoted to Ant's JUnit integration: how to use it, its limitations, and
the techniques to make seamless integrated testing part of every build.
4.1 REFACTORING
Assuming we accept the statement that all software systems must and will change over
time, and also assuming that we all want our code to remain crisp, clean, and uncluttered
of quick-and-dirty patches to accommodate the customer request du jour, how
do we reconcile these conflicting requirements? Refactoring is the answer! Refastening,
as defined by Fowler, is the restructuring of software by applying a series of internal
changes that do not affect its observable behavior (Fowler 1999).
Refactoring is one of the primary duties in agile methodologies such as eXtreme
Programming. How can we facilitate constant refactoring of our code? Some of the
key ways this can become easier is to have coding standards, simple design, a solid
suite of tests, and a continuous integration process (Beck 1999). In an eXtreme Programming
team, the names of the refactorings "replace type code with strategy" can
become as commonplace as design patterns such as "the strategy pattern." Fowler's
definitive Refactoring book provides a catalog of refactorings and when and how to
apply them, just as the "Gang of Four" book (Gamma et al. 1995) is the definitive
guide to design patterns.
We are not going to tell you how you should write your Java programs; instead,
we refer you to some of the books in the Bibliography, such as The Elements of Java
Style (Vermeulen et al. 2000) and Bloch's Effective Java (2001). These should be on
the desk of every Java developer. We address Ant coding standards in appendix D. Just
as good Java code should be simple, testable, and readable, your build file should be
simple, testable, and follow coding standards; the XP methodology applies to build
files and processes as much as to the Java source.
The remainder of this chapter is all about how to use Ant for testing. Continuous
integration is a topic that will be touched upon in this chapter, but covered in more
detail in chapter 16.
4.2 JAVA MAIN() TESTING
A common way that many Java developers exercise objects is to create a main method
that instantiates an instance of the class, and performs a series of checks to ensure that
the object is behaving as desired. For example, in our HtmlDocument class we define
a main method as
public static void main(String args[]) throws Exception {
HtmlDocument doc = new HtmlDocument(new File(args[0]));
System.out.println("Title = " + doc.getTitle());
System.out.println("Body = " + doc.getBodyText());
}
We are then able to run the program from the command-line, with the proper class-path
set:
java org.example.antbook.ant.lucene.HtmlDocument
test/org/example/antbook/ant/lucene/test.html
Using Ant as a Java program launcher, we can run it with the <java> task:
<java classname="org.example.antbook.ant.lucene.HtmlDocument">
<arg value="test/org/example/antbook/ant/lucene/test.html"/>
<classpath refid="test.classpath"/>
</java>
Writing main method checks is convenient because all Java IDEs provide the ability
to compile and run the class in the current buffer, and certainly have their place for
exercising an object's capability. There are, however, some issues with this approach
that make it ineffective as a comprehensive test framework:
- There is no explicit concept of a test passing or failing. Typically, the program
outputs messages simply with System.out.println;the user has to look at
this and decide if it is correct.
- main has access to protected and private members and methods. While
you may want to test the inner workings of a class may be desired, many tests
are really about testing an object's interface to the outside world.
- There is no mechanism to collect results in a structured fashion.
- There is no replicability. After each test run, a person has to examine and interpret
the results.
The JUnit framework addresses these issues, and more.
1 Extreme Programming Explained, Kent Beck, page 57
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.