CSE 373 Spring 2005

Homework #3

Tools for Unit Testing

April 14, 2005

Starter files

Due:

electronically Tuesday, April 19, 8:00 pm; paperwork at beginning of class Wednesday.  Note the special deadline, to avoid colliding with the midterm on April 22.

Objectives

Description

In recent years, a framework for testing called JUnit has become popular among Java programmers, both professionally and academically.  JUnit facilitates unit testing, that is, testing of individual classes and methods.  Unit testing is distinguished from system testing, in which an entire application is executed.  The goal in unit testing is to find as many bugs as possible within the individual small components which an application is built out of.

For this homework, take the TestDocument and TestSpellChecker classes and turn them into JUnit test cases.  Call these new classes DocumentTest and SpellCheckerTest.  You should preserve all the tests that were there originally and add more if appropriate.  You can start from your own versions of TestDocument and TestSpellChecker or use any version that we gave you.  As starting points you may also use the solutions of other students in the class, provided that

For simplicity, make the JUnit tests conform to the specifications and packaging to Homework 1, i.e., part of the SimpleSpeller package.  Of course, you will need to have Homework 1 versions of MyDocument and MySpellChecker.  These do not need to be turned in.  Write your tests so that they would compile and run with any correct implementations of MyDocument and MySpellChecker.

The site www.junit.org has downloads and examples (though they tend to make it seem more complicated than it is).  A local copy of junit.jar, which must be available to the Java compiler, is in the starter code directory.  This is actually the only thing you have to download to use JUnit.  The main idea is that you write "test case" classes which extend the TestCase class.  Within these classes, write methods whose names begins "test...", with void return, and no arguments.  If you run within and IDE such as DrJava or Eclipse, such methods will be discovered and run semi-automatically.  You do not need to write a main method.

The JUnit framework provides a family of assert methods, like assertTrue which are useful in testing conditions and reporting errors.  Since JUnit was first invented, an assert keyword (a statement) has been added to the Java language.  assert  can be used with JUnit, but can also be used independently.  Some programmers prefer to use the assert statement in their JUnit tests, instead of the various assert methods.  In this homework, you are required to use the assert statement at least in a few places, in order to demonstrate that you know to use and enable it.  You may also use the JUnit assert methods if you wish.

Unfortunately, Sun released Java 1.4 with the assert statement disabled by default.  Enabling it requires two separate operations:

IDEs differ in how they handle these options.  In some cases you may not have to do anything (it is handled automatically).  In other cases, you have to at least tell the IDE that you want assertions enabled.  In one really inconvenient scenario, running under Eclipse, you have to manually type in -ea for every single test configuration, in a dialog box that is not easy to find (it is not part of "Preferences").  Added 4/15: Instructions on configuring Eclipse from a blogger named Jeff Duska (thanks "Kleb" for finding this).  There's a slight typo on the page: use -ea (with a dash) instead of just ea.

To test whether assertions are enabled, cut and paste the following code into your solution (both classes) and make sure it executes.  When things are set up properly you will get the "assert is enabled" message.  Leave this code in the classes you turn in.

		try {
assert 1 == 0: "does 1 equal 0?";
System.out.println("assert is not enabled!");
System.out.println("Figure out how to enable it (-ea from the command line)" +
" and try again");

} catch (AssertionError e) {
System.out.println("assert is enabled.");
}

An interesting footnote is that JUnit is a community development.  "Community" here implies that it was developed spontaneously by a small group of individuals, with no official status, and then cooperatively developed into the tool it has become.

Just as before (and always): You must use the exact names for classes and methods as specified here and in any starter code (etc. etc.).

Getting Started:

Turn in:

Electronically: two .java files:  Click Here to Turn-in.  Recommendation: turn in early and often!
On paper: a printed copy of the "receipt" from your turn-in, which automatically includes printouts of the .java files.  Within those printouts, circle boldly or highlight the assert test code.  Plus: a short report which

Grading

Most of the grading points will come from
Good luck and have fun!


Notes
If you have questions along the way, either about the assignment or how to do things, feel free to discuss it on the Message Board (when it is operational!).  Just don't give away code, or any crucial aspects of the solution.  It is perfectly OK to share insights about using the Java libraries.

Where applicable, write your code to be reusable and modifiable.  In a later homework, you might in fact have an opportunity to reuse or modify code from this one...