©2011 Viera K. Proulx
One of the mainstays of professional Java programmers is JUnit. There are several testing libraries available for many programming languages. Before a programmer can use JUnit effectively, she needs to understand how to implement/override correctly the methods that compare two objects for equality.
In this lab we learn how to define the equals method correctly (with concurrent compatible definition of the hashCode method) by examining what happens when one or the other is not defined correctly.
We are now ready to also use the professional testing framework JUnit. We learn how to run the simplest tests in JUnit, so later we can explore its full power.
Start a new project, download the file Lab10-sp10.zip, unzip it and import all files into the new project.
Now work on Lab 10 from Spring 2010 (abridged, to focus only on the issues we are interested int).
Just for a minute we would like to consider how to compare two sets (implemented as Java Set) for equality. It makes no sense here to use the value-based equality of two items, as the add method in the Set class relies on the implementation of the equals method to eliminate duplicates. As the result, the tester library respects this implementation and compares two Sets by verifying that the two sets have the same size, and then making sure that the second set contains every item in the first set.
There is no work fot you here - you may want to look up the samples for this case, run them and contemplate.