1
我想使用Junit它測試如果我的堆棧工作正常。我得到的輸出:Junit測試堆棧彈出
testPopEmptyStack(StackTesting.TestJunitStack): null
false
不過,我希望得到一個輸出true
因爲在我的堆棧類。如果pop()
堆棧中沒有nodes
,我希望它可以throw new EmptyStackException()
。
堆棧類:
public class Stack {
Node top;
int count = 0;
ArrayList<Node> stack = new ArrayList<Node>();
public boolean checkEmpty() {
if (count == 0) {
return false;
}
else {
return true;
}
}
public Node getTop() {
if (count > 0) {
return top;
}
else {
return null;
}
}
public void pop() {
if (count > 0) {
stack.remove(0);
count--;
}
else {
throw new EmptyStackException();
}
}
public void push(int data) {
Node node = new Node(data);
stack.add(node);
count++;
}
public int size() {
return count;
}
}
TestJunitStack.java:
public class TestJunitStack extends TestCase{
static Stack emptystack = new Stack();
@Test(expected = EmptyStackException.class)
public void testPopEmptyStack() {
emptystack.pop();
}
}
TestRunnerStack.java:
public class TestRunnerStack {
public void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunitStack.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
EDIT
static
從testPopEmptyStack
刪除
刪除
static
輸出是一樣的= [ – Liondancer試試我的簡單測試@Test(預期= EmptyStackException.class) 公共無效testPopEmptyStack(){ 拋出新的EmptyStackException(); } –
嗯奇怪,它有相同的輸出。我喜歡這個測試案例 – Liondancer