Lab 6: 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.

Assignment

AutoCompleter interface

The AutoCompleter interface has the following methods:

All words stored by an AutoCompleter implementation for this assignment should be converted to lowercase. As a result, duplicate detection, exact matching, and prefix matching are case-insensitive. For example, "Dog", "DOG", and "dog" represent the same stored word, and a prefix of "Al" should match a stored word such as "alpha".

The format() method should select the largest time unit for which the first displayed value is at least 1. Use the following output forms:

Nanosecond valueOutput format
less than 1 microsecondwhole nanoseconds
less than 1 millisecondmicroseconds to one decimal place
less than 1 secondmilliseconds to one decimal place
less than 1 minuteseconds to one decimal place
less than 1 hourwhole minutes and seconds to one decimal place
less than 1 daywhole hours, whole minutes, and whole seconds
1 day or morewhole days, whole hours, and whole minutes

Examples include:

Values should remain in the unit selected by the original nanosecond value, even if rounding causes the displayed value to reach the next unit. For example, 999_999 nanoseconds should be displayed as 1000.0 microseconds, not 1.0 millisecond.

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.

The constructor must:

You may assume that the List passed to the constructor is modifiable and does not contain null elements.

To ensure there are no duplicates, you can make use of the distinct() stream method. If you have a list called items you can do the following to remove duplicates:
List<String> unique = items.stream().distinct().toList();
items.clear();
items.addAll(unique);

Remember that for this assignment the stored words should be lowercase before duplicates are removed.

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 is not completely specified, please ask your instructor for clarification.

Your tests should consider normal cases, boundary cases, invalid input, and behavior that may change as data is added to the auto completer. A test suite that demonstrates only that a correct implementation works is not necessarily thorough; your tests should also be capable of distinguishing correct behavior from plausible incorrect implementations.

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.

See your professor's instructions for details on submission guidelines and due dates.