【发布时间】:2019-01-26 13:05:22
【问题描述】:
我正在为 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 个测试,因为我无法理解这些错误的含义。有谁知道这些错误可能是什么?
我应该编写什么样的测试来捕获这些错误?
谢谢 -一个绝望的学生
【问题讨论】:
-
我们不需要查看被测方法本身的实现。事实上,当谈到设计测试时,这会让人分心。您需要考虑的是方法需要满足的规范,包括如何处理错误和异常情况的规范。这很大程度上决定了您编写哪些测试。
-
我的水晶球对于您提供的具体失败消息有点模糊,但我的猜测是它们针对的是被测方法不适用于第一个和/或列表的最后一个元素。或者,他们几乎听起来像是在期待某种列表结构已损坏的案例的恢复。
-
谢谢你们@JohnBollinger 的回复,请你给我一个例子,并解释你的意思,以便我可以根据它编写一个测试'
-
问题中添加了方法的说明
标签: java testing junit linked-list doubly-linked-list