CMPU 102 - Lab 2

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

Getting started

Start a web browser using the small Firefox button on the task bar at the bottom of the screen, or by typing

firefox &
in a terminal window and hitting return.  Go to the class web page, scroll down to the "Labs" section, and download the file into your home directory.

Start Eclipse by typing

eclipse &
in a terminal window and hitting return.  Once it starts up, choose "File->Import->Existing Projects into Workspace" from the menu, click "Select archive file", select the file "" in your home directory, and click the "Finish" button.  You will see a new project called "lab2" in the Package Explorer.

Unit tests

One of the most important things you do when implementing any class is to test the class thoroughly.  One good way to do this is by writing unit tests.  A unit test is a small chunk of code that calls one or more methods of a class, and make sure they work correctly.  Unit tests generally work by providing some input to the method(s) and checking that the output is correct.

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 the Package Explorer, expand "lab2", then the "" package, and then double click on "".  TestDate is a Java class that contains several unit tests for the Date class.  Here is how the class works:

Take a look at the three test case methods and make sure you understand what they are doing.

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, ""), 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.  (Note: on the Suns, this bar may appear to be gray.)

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.

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 me to verify this before you submit your work.)

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

Note that a single method may have multiple bugs.

Submitting your work

In the Package Explorer, select the "lab2" project.  From the menu, choose "File->Export...->Archive file", click "Next", and save the project to a file called "" in your home directory.

Open a new terminal window, and create a new directory called "lab2" by typing the following command and hitting return:

mkdir lab2

Now move your saved project file into the "lab2" directory:

mv lab2

Finally, run the submit102 command to submit the "lab2" directory and its contents:

submit102 lab2