CMPU 102 - Lab 2

$Revision: 1.4 $

In this lab, we will develop test cases for a buggy implementation of the Date class from Assignment 2.

Getting started

Open a web browser and go to the class web page:

http://www.cs.vassar.edu/~cs102/

Scroll to the bottom of the page and download the file lab2.zip into your home directory.  In a terminal window, start Eclipse with the command

eclipse &

Import "lab2.zip" into your Eclipse workspace ("File->Import...->Existing Projects into Workspace").

Unit tests

A unit test is a small chunk of Java code that tests whether a Java method call returns the correct value when given a particular input.  By testing each method in a class with a variety of inputs, we gain greater confidence that the class is implemented correctly.

Writing unit tests with JUnit

JUnit is a popular software package for developing unit tests in Java.  Eclipse has built-in support for running JUnit tests.

In this lab, the source file "DateTest.java" defines a suite of unit test for the Date class from Assignment 2.  Here is how the DateTest class works:

Take a look at the DateTest class---it should be fairly self-explanatory.

Running JUnit tests

Eclipse makes it very easy to run JUnit tests cases.  Just right click on the name of the file containing the test cases (in this case, "TestDate.java"), and choose "Run as->JUnit test".  On the left side of the Eclipse window (where the Package Explorer normally appears) you will see a window that looks something like this:

The green bar indicates that all three test cases passed.  So, for the moment, we haven't yet found any bugs.

Your task: write tests to find bugs

Your task is to add new test case methods in order to identify some bugs in the implementation of the Date class.

If you are testing a static method, such as isLeapYear, you don't need to add any new fields.  However, if you want to test a non-static method (such as getDaysRemainingInMonth), then you should create a new field of type Date, and modify the setUp method to initialize it with a new Date object.  Add as many new Date objects as you need.

Here is what the JUnit window will look like when a test case causes a failure:

The bar turns red when one or more test cases fail.

You are done when you have found two or more bugs.  You should have at least two test cases that fail, each of which demonstrates a different bug in the Date class.  (Please ask an instructor or lab coach to verify this before you submit your work.)

Bonus: there are at least 4 bugs.  You will receive a 10% bonus on the lab if you find 4 or more bugs.

Note that a single method may have multiple bugs.

Rules for Writing Unit Tests

Please follow these rules when writing unit tests:

  1. Do not modify or delete test objects or unit test methods.  If you want to test a Date object that is constructed differently than the existing ones, create a new Date object.

  2. Only use one or two assertion calls in each unit test method.  When there are too many assertions made in a unit test method, it becomes difficult to understand what has happened when one of the assertions fails.

Submitting your work

In a terminal window, run the following commands:

cd
cd eclipse-workspace
submit102 lab2