当我试图在一个特殊类型的类中测试一个方法时,我得到一个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;
}
}
我想运行一个测试,返回列表中元素的值。