CPE 102
Winter
2008
Lab 4
Errata:
None – any updates or corrections will appear here.
Objectives
-
Practice
writing code to perform unit testing on previously written code
-
Practice
handling possible exceptions
-
More
practice with the equals method
-
More practice using the JUnit testing framework
Resources
Ground
Rules
-
You
may want to work in groups to complete this lab. If you do, be sure you feel confident
that you could complete it on your own – i.e., you fully understand the
material.
Orientation
You will be provided with a compiled (.class) file for a
Java class called SomeClass. This class has a single constructor
(documented below) and an overridden implementation of the equals() method from
Object. Your job is to develop unit tests for the equals() method and
discover several bugs that exist with its implementation. Your knowledge
of what the equals() method should do is vital to developing an efficient and
exhaustive unit test. Through lectures,
your text, and the projects to-date you should have learned how a
well-implemented equals method behaves – if you haven’t you should talk it
over with your peers and/or your instructor before you begin to develop the
unit test code. Here is the
documentation for the SomeClass constructor:
/**
* Constructor
for SomeClass
* @param x
the x-value (may be any valid integer value).
* @param y the
y-value (may be any valid integer value).
* @param s
the String value (may be a String or null).
*/
How to
Proceed
-
Think about the problem
before you begin writing code. What
problems are you looking for in the equals? How many different unit tests will you need for
complete code coverage? What
cases should these tests cover? Is there
any way to organize the test code to make it thorough and concise? Time spent
up-front will usually save you time later!
-
Create a new project in your
chosen IDE and create a java class called SomeClassTest. In a format similar to previous labs, create and
run your JUnit test cases through a test driver named AllTests.
For the time being, do not add any tests to SomeClassTest; merely compile
it so you can see where your IDE places the .class files. To do so, use Window Explorer and inspect
your file system to see where SomeClassTest.class has been created.
-
Place a copy of SomeClass.class in the same directory
that your IDE puts its .class files for your project. Do not write the
SomeClass class – you must use the provided SomeClass.class file so that
you can find the significant implementation errors the provided version
contains.
-
In the body of SomeClassTest, develop unit tests to construct various SomeClass objects (and
other objects, if necessary) to test its equals() method. The parameters to the constructor are
provided in the Orientation section above. You may write additional methods to
facilitate testing if you wish. Your
code should handle (catch) any exceptions that may occur during
testing. Each unique unit test must
have a meaningful method name and a javadoc comment that describes what the test is doing, for example:
/**
* Test to make sure the x instance variable is checked by the equals
method
*/
@Test
public void testXVariable()
{
SomeClass sc1 = new SomeClass(1, 5, "Whatever");
SomeClass sc2 = new SomeClass(2, 5, "Whatever");
assertFalse(sc1.equals(sc2));
}