我正在为Junit编写测试,以测试我编写的删除功能:
/**
* Deletes the item at the given index. If there are any elements located at a higher
* index, shift them all down by one.
*
* @throws IndexOutOfBoundsException if the index < 0 or index >= this.size()
*/
@Override
public T delete(int index) {
if (index < 0 || index > this.size()) {
throw new IndexOutOfBoundsException();
} else if (isEmpty()) {
throw new EmptyContainerException();
} else {
Node<T> current = front;
if (index == 0) {
front = current.next;
current.prev = null;
size--;
return current.data;
} else if (index == size - 1) {
return remove();
} else {
current = traverse(current, index);
Node<T> temp = current;
current.prev.next = current.next;
current.next.prev = current.prev;
size--;
return temp.data;
}
}
}
此方法用于同时具有后节点和前节点的双链表。
问题:我们的学院将针对我们编写的测试运行错误代码,以确定我们是否编写了足够的测试来捕获不良代码和异常。
我知道它们将运行其中的2个测试,但不知道错误意味着什么。
失败:MissingBackFieldRepairLogic
Unable to find bug with DoubleLinkedList with missing back field repair logic
失败:MissingNextNodeRepairLogic
Unable to find bug with DoubleLinkedList with missing next node repair logic
这些^是2个测试,由于无法理解这些错误的含义,因此我没有进行说明。有谁知道这些错误可能是什么?
我应该编写什么样的测试来捕获这些错误?
谢谢 -一个绝望的学生
答案 0 :(得分:0)
Javadoc不一定与规范相同,但是假设您拥有Javadocs,或者假设所提供的文档充分捕获了全部规范,那么我将测试以下情况:
错误案例
在每种情况下,请验证是否抛出了正确的异常。
正常情况
在每种情况下,请验证是否返回了正确的对象,并且列表中剩余的元素正确且顺序正确。
目前尚不清楚您明确询问的两种情况的含义,但我想这与删除期间保持列表内部一致性的缺陷有关。您如何检查此类缺陷取决于列表的结构,列表提供的方法以及列表暴露的内部细节(如果有)。