Lab 7: JUnit Testing
Overview
Having thorough tests can ensure the behavior of a class matches its
specification, but writing thorough tests can be challenging. In this
assignment you will gain experience writing unit tests for an AutoCompleter
interface and use it to test your own implementation of the interface.
Video introduction for the lab
Commit 1 - AutoCompleter Comments
Due end of class Tuesday Week 7
AutoCompleter
interface
The AutoCompleter
interface has the following methods:
boolean add(String word)
— returnstrue
ifword
is added to the object (a word should not be added if it is already in the auto completer). Ifword
isnull
or an empty string, anIllegalArgumentException
is thrown.int size()
— returns the number of items in the auto completer.boolean exactMatch(String target)
— returnstrue
iftarget
is found in the auto completer. Iftarget
isnull
or an empty string, the method returnsfalse
.String[] allMatches(String prefix)
— returns an array of all the strings in the object that begin with theprefix
. Ifprefix
is an empty string, an array of all the strings in the auto completer are returned. Ifprefix
isnull
, an empty array is returned.String getBackingClass()
— returns aString
indicating the fully qualified name of the data structure used to store the words for the AutoCompleter. E.g., "java.util.ArrayList".static String format(long nanoseconds)
— returns a human-friendly string representing the number ofnanoseconds
. The format of the string must be consistent with the examples below. Select the first line where the first number is at least1
. Note: this method is declaredstatic
which means that it should be implemented in the interface. If a negative value is supplied, the method should throw anIllegalArgumentException
.- 2 day(s) 5 hour(s) 32 minute(s)
- 14 hour(s) 22 minute(s) 8 second(s)
- 42 minute(s) 55.3 second(s)
- 18.8 second(s)
- 998.8 millisecond(s)
- 318.8 microsecond(s)
- 7 nanosecond(s)
You must implement the static format()
method and write complete javadocs for all the methods in AutoCompleter
.
For this commit, rename the package and write JavaDoc comments for all methods of the interface.
Commit 2 - AutoCompleter Comments
Due end of class Tuesday Week 7
Implement the AutoCompleter.fortmat()
method.
Commit 3 - UnorderedList partial implementation
Due end of class Wednesday Week 7
Unordered List Implementation
You must create a class, UnorderedList
, that implements the AutoCompleter
interface using an unordered list. Your class must have only one private
attribute: private final List<String> items
that is assigned via a
one-argument constructor. Note: the constructor should ensure that no
duplicates are in the list passed to the constructor.
For this commit, you must create method stubs for all of the methods and
implement the getBackingClass()
method.
Commit 4 - getBackingClass() tests
Due end of class Wednesday Week 7
Testing
You must create JUnit tests for all of the methods in the AutoCompleter
interface. These tests should be rigorous and ensure all methods behave as
specified in the requirements. If the behavior of specific methods are not
completely specified, please ask your instructor for clarification.
Use the tests to ensure your UnorderedList
implementation conforms to the requirements.
Your instructor may verify your tests by providing multiple faulty implementations to see if your tests catch the errors embedded in the faulty implementations.
Determine and implement the appropriate methods to test the getBackingClass()
method.
Commit 5 - UnorderedList add() and size() implementations
Due end of class Thursday Week 7
Implement the constructor, UnorderedList.add()
and UnorderedList.size()
methods.
HashSet
. If you
have a list called list
you can do the following to remove duplicates:
Set<String> unique = new HashSet<>(list);
list.clear();
list.addAll(unique);
Commit 6 - Additional tests
Due end of class Monday Week 8
Implement the UnorderedList.add()
and UnorderedList.size()
methods.
Commit 7 - Lab completed
Due end of class Monday Week 8
- Implement tests and
UnorderedList
implementations of the remaining methods in theAutoCompleter
interface - Add a block comment at the end of the
AutoCompleterTest
class discussing the following: "What method did you find most difficult to test? Why?"
Acknowledgement
This laboratory assignment, developed by Dr. Chris Taylor.