Lab: Testing toStringWithCommas With JUnit
Objective
Your goal for this lab is to expose the defects in several
implementations of the NaturalNumber static method toStringWithCommas.
Setup
For this assignment you need to import a jar archive.
Download the file
NNtoStringWithCommas.jar and save it on your local disk.
Now follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying ProjectTemplate.
Name the new project TestingToStringWithCommas. You will
not need any of the source files in the src folder.
- Instead import the jar archive you downloaded
into the lib folder of this project by following these
steps:
- Right click on the lib folder in the Package
Explorer view and select Import... from the pop-up
menu;
- Select General > File System and click Next;
- In the From directory enter the location on the local
disk drive of the file you downloaded earlier;
- In the list of files check the check-mark next to NNtoStringWithCommas.jar
and make sure that the Into folder box shows TestingToStringWithCommas/lib,
then click Finish.
- Finally, in the Package Explorer expand the lib
folder, right-click on the NNtoStringWithCommas.jar file,
and select Build Path > Add to Build Path. You should
double check that NNtoStringWithCommas.jar is now listed
under Referenced Libraries in your project.
Method
- Start by creating a new JUnit test fixture and call it NNtoStringWithCommasTest
(see the last
lab for details). Make sure you put the test fixture in the test
folder and for the class under test use NNtoStringWithCommas1.
This is the one correct implementation of toStringWithCommas
provided in the NNtoStringWithCommas.jar archive.
- Open the NNtoStringWithCommasTest.java file in the
test folder and copy and paste the following method into
it: In your test cases, you should call this method instead of calling
directly the method under test (e.g., NNtoStringWithCommas1.toStringWithCommas).
This way you can replace the implementation under test by simply
changing the name of the class in the body of the method above.
This is an example of the application of a fundamental principle
in software design known as single point of control over
change. The idea is to design software so that a change in a
design decision can be effected by a change in a single place in
the code.
- Copy and paste to the test fixture the test cases you
designed for today's homework.
- Once you have entered all your test cases and the test
fixture compiles, run it (see the last
lab for details). All the test cases should pass because the
implementation of toStringWithCommas in NNtoStringWithCommas1
is correct. If any test cases fail, that indicates a bug in the
test case code. Fix all the test cases before moving on to the
next step.
There are five more classes implementing toStringWithCommas
in the NNtoStringWithCommas.jar archive and they are named
NNtoStringWithCommas2, NNtoStringWithCommas3, NNtoStringWithCommas4,
NNtoStringWithCommas5, and NNtoStringWithCommas6.
They all have at least one bug.
- Test the first defective implementation by replacing NNtoStringWithCommas1
with NNtoStringWithCommas2 in the body of the private
method redirectToMethodUnderTest and running the test
fixture.
- If any of the test cases fail, record the
input(s) and the observed output(s) on a sheet of paper and
move on to the next implementation.
- If none of your test cases fail, that indicates that
your test fixture can and should be improved. Think of situations
you have not considered in your current test plan and add
one or more new test cases. Rerun the test fixture. Repeat
until you find a test case that breaks the implementation.
- If you get stuck on one implementation you cannot break,
feel free to try your test fixture on the other implementations
and come back to this one later on. However, the goal for this lab
is to find at least one test case for each defective
implementation that shows the presence of a bug.
- Once you believe you have broken every implementation, call
an instructor to show the test cases you identified to break each
implementation.
Additional Activities
Now that you have a test plan (and JUnit test fixture) for toStringWithCommas,
it would be a good exercise to use it to test your own
implementation of this method from an earlier
lab.
- In the Package Explorer view, copy the file NaturalNumberStaticOps.java
from the src folder in the RecursionOnNaturalNumber1
project and paste it into the src folder of this project.
- If you already implemented toStringWithCommas as
part of the Additional Activities of the earlier lab, just move to
the next step. Otherwise provide an implementation for the method
now.
- Replace the call to toStringWithCommas in redirectToMethodUnderTest
with a call to your implementation in NaturalNumberStaticOps
and run the JUnit test fixture. Fix any defects you discover until
your implementation passes all the tests.
- How confident are you that you have a correct
implementation of toStringWithCommas? Justify your
answer.