我似乎无法弄清楚为什么我的一项测试失败了。
这是测试:
@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
下返回false 。super
关于发生了什么的任何想法?谢谢
相关代码:
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();
}
}