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));
}
我已经设置了一个断点,并多次完成该过程。它进入类中的第二个 if 语句,并且似乎抛出异常。然后,该过程返回到类,并在块下返回 false。ChessPiece
Rook
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();
}
}