尝试测试具有特殊类型的类中的方法时,我收到 NullPointerException。关于这个例外的原因,我是我们的想法。
public class TestStack {
private Stack st;
private Entry en;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
st = new Stack();
en = new Entry(1);
}
@Test
public void pushThenTop() {
st.push(en);
assertEquals("TEST1: push then Top", 1, st.top());
fail("Incorrect type");
}
}
堆栈类
public class Stack {
private int size;
private List<Entry> entries;
public void push(Entry i) {
entries.add(i);
}
public final Entry pop() throws EmptyStackException {
if (entries.size() == 0) {
throw new EmptyStackException();
}
Entry i = entries.get(entries.size() - 1);
entries.remove(entries.size() - 1);
return i;
}
public Entry top() throws EmptyStackException {
return entries.get(entries.size() -1);
}
public int size() {
size = entries.size();
return size;
}
}
我正在尝试运行一个返回列表中元素值的测试。