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
Assignment
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
.
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.
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);
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.
Acknowledgement
This laboratory assignment, developed by Dr. Chris Taylor.