我必须为这个方法编写一个测试。我尝试过不同的测试方法,但都失败了。
这是给我的方法,根本无法改变。
/**
* Tests if another BoardPiece has the same value as this BoardPiece.
* @param other the BoardPiece to test against this BoardPiece.
* @return true if the BoardPieces have the same value, false otherwise.
*/
public boolean equals(BoardPiece other)
{
return this.value.equals(other.value);
}
这是我测试它的一次尝试。
@Test
public void testEquals() {
BoardPiece boardPiecetest = new BoardPiece("value");
boolean q = this.value.equals(boardPiecetest);
assertTrue(q);
}
我想你想要这样的东西:
@Test
public void testPositiveEquals() {
BoardPiece boardPieceOne = new BoardPiece("value");
assertTrue(boardPieceOne.equals(boardPieceOne));
}
@Test
public void testNegativeEquals() {
BoardPiece boardPieceOne = new BoardPiece("value");
BoardPiece boardPieceTwo = new BoardPiece("no value");
assertFalse(boardPieceOne.equals(boardPieceTwo));
}
我假设'this.value'的类型是String。所以本质上,你在测试的第三行将一个约束与一个BoardPrice进行比较。