CS 151

Cay S. Horstmann
Lecture 7
- Pre-class reading: TODO
- Clicker questions
- Lecture (Slides are here)
- Lab
- Discussion
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?
- This is a violation of encapsulation.
- This is a violation of the Law of Demeter.
- This public interface is not cohesive.
- These methods have a side effect.
Lecture 7 Clicker Question 2
What can a method do when a precondition is not fulfilled?
- Throw an
AssertionError
- Throw an exception
- Set the object's data representation to an invalid state
- Any of the above
Lecture 7 Clicker Question 3
Which of the following are true about JUnit 4 unit tests? Check all that apply.
- A unit test must be a public method
- A unit test must be annotated with
@Test
- A unit test returns
true
if it passes
- A unit test throws an exception if it fails
Lab

- Work with a buddy
- One of you (the “coder”) codes, the other (the “scribe”) types up the answers.
- One of you submits the code in the
lab2
subdirectory of your personal repo, the other submits a file report.txt
in the lab2
subdirectory of your repo.
- Put the name of the coding buddy into the report
- Push your repo when you are done
- You don't have to complete all parts, but you have to be present and do your best.
Unit Tests
- Coder: Download this file. Then make a new directory and put it in there.
mkdir ~/cs151/lab7
mv ~/Downloads/HashSet.java ~/cs151/lab7
- 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.
- 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?
- Complete the test to iterate over the other elements. What is the test? (It should pass.)
A Failing Test
- 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?
- 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?
- This page has a correction (look for Page 780). Try it out. Does the unit test now pass?
- Is the corrected version correct?
- If you have time...why did the original version misbehave?
Calling Unit Tests from the Command Line
- 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
- 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?
- That's what the grader will do with the next homework.
Discussion
