DS3: JUnit Design Studio
10 points
September 19, 2016

Overview

This design studio helps you get started with writing JUnit tests.


1. Preparatory tasks

  1. Read the slides on inspections and software testing, black box testing, and JUnit.

  2. Read the documentation of the Java HashMap class. In particular, focus on the specifications of the methods for which you will write JUnit tests in class. The methods are listed in the next section.

  3. Your team must have at least one laptop to work on this design studio in class. Remember to pack your laptop!


2. In-class tasks

You will work on these tasks as a team. There will be one submission per team. We will pass out an attendance sheet where each student will write his/her name.

  1. In Eclipse, create a Java project called DS3.

  2. In this project, create a new JUnit Test case class by going to File→New→JUnit Test Case.

  3. Use the default package for this studio. Note that in general, your JUnit test case class should be in the same package as the class it is supposed to test.

  4. Give the name HashMapTest. For simplicity, do not select the method stubs for setup and teardown for this design studio.

  5. For class under test, type HashMap and browse. Then select java.util.HashMap.

  6. Eclipse will ask you to add the JUnit 4 library path to your project's build path. You must agree.

  7. You will see a blank method called test(), with the fail method.

  8. At the top of the file, add the names of the participating team members as comments.

  9. You will add new test methods that will test the following methods of the HashMap class.

    1. V get(Object key)
    2. boolean isEmpty()
    3. V put(K key, V value)
    4. V remove(Object key)
    5. V replace(K key, V value)

    Each test method should test only one method in the HashMap class, and only one aspect of it. Use the testing strategies that we covered in previous lectures. For example, you may want to test using an empty HashMap in one test case and a non-empty HashMap in another. To test the put method, use values that are already in the HashMap in one test case, and a value that is not in the HashMap in another. Try out various inputs from equivalence classes as well as boundary cases. You will end up with about 4-5 test methods for each of the five methods listed above.

    Your tests must use all of the following assertions in some way.

  10. Your test code should compile and run. To Run, right click on the HashMapTest.java file in the package explorer and "Run as" JUnit Test. You will see a green bar if the tests pass or a red bar if a test fails.

  11. Submit your file HashMapTest.java using the Canvas submission named DS3, which has been set up for this Design Studio. The file must be submitted by the end of the lecture, otherwise, Canvas will not accept the submission and you will not get any credit for the design studio.