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 projectTestingToStringWithCommas. You will not need any of the source files in thesrcfolder. - Instead import the
jararchive you downloaded into thelibfolder of this project by following these steps:- Right click on the
libfolder 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 and click Select Folder;
- In the list of files check the check-mark next to
NNtoStringWithCommas.jarand make sure that the Into folder box showsTestingToStringWithCommas/lib, then click Finish.
- Right click on the
- Finally, in the Package Explorer expand the
libfolder, right-click on theNNtoStringWithCommas.jarfile, and select Build Path > Add to Build Path. You should double check thatNNtoStringWithCommas.jaris now listed underReferenced Librariesin 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 thetestfolder and for the class under test useNNtoStringWithCommas1. This is the one correct implementation oftoStringWithCommasprovided in theNNtoStringWithCommas.jararchive. -
Open the
NNtoStringWithCommasTest.javafile in thetestfolder and copy and paste the following method into it:/** * Calls the method under test. * * @param n * the number to pass to the method under test * @return the {@code String} returned by the method under test * @ensures <pre> * redirectToMethodUnderTest = [String returned by the method under test] * </pre> */ private static String redirectToMethodUnderTest(NaturalNumber n) { return NNtoStringWithCommas1.toStringWithCommas(n); }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
toStringWithCommasinNNtoStringWithCommas1is 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
NNtoStringWithCommas1withNNtoStringWithCommas2in the body of the private methodredirectToMethodUnderTestand 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.javafrom thesrcfolder in theRecursionOnNaturalNumber1project and paste it into thesrcfolder of this project. - If you already implemented
toStringWithCommasas 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
toStringWithCommasinredirectToMethodUnderTestwith a call to your implementation inNaturalNumberStaticOpsand 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.