Testing removeAny


Here's a plan for robustly testing removeAny. It fits in with the plan we recommend for testing any public instance method. That plan outlines that we should have exactly three sections in each test method:

  1. The Setup, in which initial values are established for the "expected" object and the "actual" object.
  2. The Call (of the method under test on the "actual" object); the section contains exactly one statement.
  3. The Evaluation, in which some or all of the statements are calls to assertEquals, but not all of the statements must be such calls.

You will help your graders and other readers immensely by using a comment to head each of these three sections. In other words, labeling the sections is very helpful.

Here, then, is a plan for testing removeAny for a set of size two or more:

  1. The Setup: surprisingly, perhaps, make the initial value of the "expected" object equal to the initial value of the "actual" object; we'll change the value of the "expected" object later.
  2. The Call: capture the value returned.
  3. The Evaluation: First, assertEquals, expecting true, that the "expected" object contains what removeAny removed from the "actual" object. (Using assertEquals gives a better message than using assertTrue.) Second, it is now safe to specifically remove a value equal to the captured value returned by The Call from the "expected" object to establish a truly right value left behind in the "expected" object. Third, assertEquals that the "expected" object and the "actual" object have the same value now.

This plan thoroughly examines the results for any wrong values.