當我試圖在一個特殊類型的類中測試一個方法時,我得到一個NullPointerException。對於這個例外的原因,我是我們的想法。JUnit空指針異常
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");
}
}
Stack類
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;
}
}
我想運行一個測試,返回列表中元素的值。