但是,等一下 — 當我重新運行該行為時,它仍然失!
清單 15. JBehave 報告一個 null 值,而不是一個異常
1) StackBehavior should pop pushed value:
VerificationException: Expected:
same instance as <test>
but got:
null:
至少清單 15 中的失敗有別于清單 13 中的失敗。在這種情況下,不是拋出一個異常,而是沒有發現 "test" 值;實際彈出的是 null。仔細觀察 清單 10 會發現:一開始我將 pop() 方法編寫為當內部容器中有項目時,就返回 null。問題很容易修復。
清單 16. 是時候編寫完這個 pop 方法了
public E pop() {
if(this.list.size() > 0){
return this.list.remove(this.list.size());
}else{
throw new RuntimeException("nothing to pop");
}
}
但是,如果現在我重新運行該行為,我又收到一個新的錯誤。
清單 17. 另一個錯誤
1) StackBehavior should pop pushed value:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
仔細閱讀清單 17 中的實現可以發現問題:在處理 ArrayList 時,我需要考慮 0。
清單 18. 通過考慮 0 修復問題
public E pop() {
if(this.list.size() > 0){
return this.list.remove(this.list.size()-1);
}else{
throw new RuntimeException("Nothing to pop");
}
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/