CS 151

Cover page image

Cay S. Horstmann

Lecture 7

Lecture 7 Clicker Question 1

Consider a solution to Homework 3 with the following classes:

public class Question 
{
   private Answer studentAnswer = new Answer();
   private Answer instructorAnswer = new Answer();
   . . .
   public Answer getStudentAnswer() { return studentAnswer; }   
   public Answer getInstructorAnswer() { return instructorAnswer; }
   . . .
}

Which of the following statements is true?

  1. This is a violation of encapsulation.
  2. This is a violation of the Law of Demeter.
  3. This public interface is not cohesive.
  4. These methods have a side effect.

Lecture 7 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

Lecture 7 Clicker Question 3

Which of the following are true about JUnit 4 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

Unit Tests

  1. Coder: Download this file. Then make a new directory and put it in there.
    mkdir ~/cs151/lab7
    mv ~/Downloads/HashSet.java ~/cs151/lab7
  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 4). Accept the offer to add the JUnit 4 library to the project.
  3. Change the test method to:
          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?
  4. 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 twelve 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 test1()
       {
          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. Download the two “plain old JAR” from this site and put them into a directory junit4:
    mkdir ~/junit4
    mv ~/Downloads/{junit,hamcrest-core}.jar ~/junit4
    
  2. Change to the lab7 directory and compile your unit test:
    cd ~/lab7
    javac -cp .:~/junit4/\* HashSetTest.java
    java  -cp .:~/junit4/\* org.junit.runner.JUnitCore HashSetTest
    
    What happens?
  3. That's what the grader will do with the next homework.

Discussion