【发布时间】:2021-08-07 15:12:58
【问题描述】:
所以我们的作业是在我们的 won 上实现一个堆栈,然后为它编写测试用例。 这是堆栈:
import java.util.Vector;
class Stack<T> extends Vector<T> {
private Vector<T> stack;
Stack() {
stack = new Vector<T>();
}
// returns false or true, given the stack is empty or not.
public boolean isEmpty() {
return stack.size() == 0;
}
//returns the top element of the stack without removing it.
public T peek() {
return stack.get(stack.size()-1);
}
//puts a new element to the top of the stack
public void push(T element) {
stack.add(element);
}
//returns and removes the top element of the stack
public T pop() {
return stack.get(stack.size()-1);
}
}
这是我目前的测试课。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class StackTest {
@Test
void isEmpty() {
stack s = new stack<Integer>;
assertEquals(true, s.isEmpty());
}
@Test
void peek() {
Stack t = new Stack(1);
}
@Test
void push() {
}
@Test
void pop() {
}
}
我真的很难弄清楚前两种测试方法有什么问题。其他人有想法吗?
【问题讨论】:
-
第一种方法中,
stack写成小写。在第二种方法中,您使用 int 调用Stack的构造函数,但未定义该构造函数。 -
第二种测试方法并没有真正测试任何东西,它没有断言......
-
@deHaar 确保某事毫无例外地发生是一种断言。