CS 151

Cover page image

Cay S. Horstmann

Day 6

Day 6 Clicker Question 1

Every Java programmer uses the java.util.List interface. From your knowledge, is it

  1. cohesive
  2. complete
  3. clear
  4. consistent

Check all that apply.

Day 6 Clicker Question 2

What can a method do when a precondition is not fulfilled?

  1. Throw an AssertionError
  2. Throw an exception
  3. Set the object's data representation to an invalid state
  4. Any of the above

Day 6 Clicker Question 3

Which of the following are true about JUnit unit tests? Check all that apply.

  1. A unit test must be a public method
  2. A unit test must be annotated with @Test
  3. A unit test returns true if it passes
  4. A unit test throws an exception if it fails

Lab

lab

Unit Tests

  1. Coder: Download HashSet.java. Then make a new directory and put it in there.
    mkdir ~/cs151/lab6
    mv ~/Downloads/HashSet.java ~/cs151/lab6
  2. In Eclipse, make a new project that is located in that directory. Select File -> New -> JUnit Test case (may be in Other if you've never done that). Accept all the defaults (name HashSetTest, using JUnit Jupiter). Accept the offer to add the JUnit 5 library to the project.
  3. In JUnit 5, you need the following import:
    import static org.junit.jupiter.api.Assertions.*;
    (The book is still at JUnit 4.)
  4. Make a test method with these calls:
          HashSet set = new HashSet(10);
          set.add(1);
          set.add(7);
          set.add(2);
          set.add(9);
          
          Iterator iter = set.iterator();
          assertEquals(1, iter.next());
    
    Run the test. What happens?
  5. Complete the test to iterate over the other elements. What is the test? (It should pass.)

A Failing Test

  1. This class came from one of my books, and it had an embarrassing bug. Your job is to come up with a number of unit tests that tests the behavior of the iterators, with different patterns of bucket populations. Some buckets can be empty, some filled with one or more elements--this is a fertile ground for coding errors. Make three test cases that fill a table whose buckets are populated as in the following patterns:
    A [o]    B [o]     C [oo]
      [o]      []        [ooo]
      [o]      [o]       []
      [o]      []        []
      [o]      [o]       []
    Use an iterator to remove the first element. Use iterators to list all elements before and after. Here is the first test case.
       @Test public void testPatternA()
       {
          HashSet set = new HashSet(5);
          set.add(0);
          set.add(1);
          set.add(2);
          set.add(3);
          set.add(4);
          Iterator iter = set.iterator();
          assertEquals(0, iter.next());
          assertEquals(1, iter.next());
          assertEquals(2, iter.next());
          assertEquals(3, iter.next());
          assertEquals(4, iter.next());
          assertFalse(iter.hasNext());
          iter = set.iterator();
          iter.next();
          iter.remove();
          iter = set.iterator();
          assertEquals(1, iter.next());
          assertEquals(2, iter.next());
          assertEquals(3, iter.next());
          assertEquals(4, iter.next());
          assertFalse(iter.hasNext());
       }
    Does it pass?
  2. Make the other two cases. One of them should fail after removing an element. Discuss with your buddy whether your test case is ok. (Hint: Elements get added to the front of each bucket.)

Is the Correction Working?

  1. This page has a correction (look for Page 780). Try it out. Does the unit test now pass?
  2. Is the corrected version correct?
  3. If you have time...why did the original version misbehave?

Calling Unit Tests from the Command Line

  1. As described here, download the “standalone JAR” from Maven. Pick the latest stable version and look for junit-platform-console-standalone-version.jar. Put it into a directory junit5:
    mkdir ~/junit5
    mv ~/Downloads/junit-platform-console-standalone-*.jar ~/junit5
    
  2. Change to the lab6 directory and compile your unit test:
    cd ~/lab6
    javac -cp ~/junit5/\*:. HashSetTest.java
    java -jar ~/junit5/junit-platform-console-standalone-*.jar --class-path . --scan-class-path
    
    What happens?
  3. That's what the grader will do with future homeworks.

Discussion

discussion