我似乎无法弄清楚为什么我的一个测试失败。JUnit测试失败,虽然预期的异常抛出
这里的测试:
@Test(expected = IllegalArgumentException.class)
public void complainsIfFromLocIsDifferentObject() throws Throwable {
board.set(make(), 1, 3); //Creates different rook from 'piece'
assertFalse("ChessPiece Test 2", piece.isValidMove(getValidMove(1, 3), board));
}
我设置一个断点,并通过流程多次了。它进入ChessPiece
类中的第二个if语句,似乎抛出异常。该过程然后返回到Rook
类,并在super
块下返回false。
关于发生了什么的任何想法?由于
相关代码:
public class Rook extends ChessPiece {
@Override
public boolean isValidMove(Move m, IChessBoard b) {
if (super.isValidMove(m, b) == false)
return false;
// Add logic specific to rook
if(m.fromRow == m.toRow || m.fromColumn == m.toColumn)
return true;
else
return false;
}
}
public abstract class ChessPiece implements IChessPiece {
@Override
public boolean isValidMove(Move m, IChessBoard b) {
//Verify that there is a piece at the origin
if (b.pieceAt(m.fromRow,m.fromColumn) == null)
throw new IllegalArgumentException();
// Verify that this piece is located at move origin
IChessPiece piece = b.pieceAt(m.fromRow, m.fromColumn);
if (this != piece)
throw new IllegalArgumentException();
}
}
很难理解你刚才给出的部分代码是怎么回事。你能把它简化成一个简短但完整的例子吗?如果你期望'isValidMove'引发异常,为什么你还会*有一个断言(你不会到达)? – 2013-03-08 06:56:46
哦,你的'ChessPiece.isValidMove'实现是无效的代码 - 它不会返回任何东西。这表明你没有运行你发布的代码。 – 2013-03-08 06:58:06
testfailure说什么? – 2013-03-08 06:58:43