Java 将 int 更改为整数时测试失败
Java Failed Test When Changing int to integer
我有方法 electCouncillor
从数组 CouncillorsSet
中取出某个议员,将其放入某个 balcony
队列,从中获取额外的议员 balcony
并将其放回 CouncillorSet
,以确定将从数组中取出哪个议员 我有私有 int 字段 CouncillorNumber
,然后我意识到我不能使用 int,因为每次我都使用该方法我必须重置 CouncillorNumber
值并且 int 不允许 null(如果我输入 0,它将始终使用数组中的第一个议员),所以我决定将其更改为整数,但出于某种原因测试失败了,我不知道为什么。
public class Player {
private int id;
private int councillorNumber;
public void electCouncillor(Region region,GameBoard gameBoard){
//Gets a certain councillor from the array CouncillorSet
Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
//Adds that councillor to the queue Balcony
region.getBalcony().add(councillor);
//Sets the councillor on top of the Balcony in the CouncillorSet
gameBoard.setCouncillor(region.getBalcony().element());
//Removes the councillor on top of the balcony
region.getBalcony().remove();
//Removes the councillor from CouncillorSet that was added to the balcony
gameBoard.getCouncillorsSet().remove(councillorNumber);
}
public int getCouncillorNumber() {
return councillorNumber;
}
public void setCouncillorNumber(int councillorNumber) {
this.councillorNumber = councillorNumber;
}
}
这是测试
@Test
public void testElectCouncillor(){
Player player = new Player(1);
GameBoard gameBoard = new GameBoard();
//First i create 6 councillors to be used in the test
Councillor councillor1 = new Councillor();
Councillor councillor2 = new Councillor();
Councillor councillor3 = new Councillor();
Councillor councillor4 = new Councillor();
Councillor councillor5 = new Councillor();
Councillor councillor6 = new Councillor();
councillor1.setColor(Color.BLACK);
councillor2.setColor(Color.BLUE);
councillor3.setColor(Color.ORANGE);
councillor4.setColor(Color.PURPLE);
councillor5.setColor(Color.WHITE);
councillor6.setColor(Color.PINK);
//Then i add the first 4 councillors to a balcony
Region region = gameBoard.getRegionCoast();
Queue<Councillor> balcony = region.getBalcony();
balcony.add(councillor1);
balcony.add(councillor2);
balcony.add(councillor3);
balcony.add(councillor4);
//The i add the remaining two to the CouncillorSet of the GameBoard
gameBoard.setCouncillorSet(new ArrayList<Councillor>());
gameBoard.getCouncillorsSet().add(councillor5);
gameBoard.getCouncillorsSet().add(councillor6);
//This gets the first element from CouncillorSet, which in this case is the councillor5 color white
player.setCouncillorNumber(0);
player.electCouncillor(region,gameBoard);
//After doing the method first i verify that the element in top of the queue is
//now the councillor Blue
assertEquals(Color.BLUE,region.getBalcony().element().getColor());
//Then i verify that the elements 0 and 1 from the CouncillorSet are the
//Pink and Black councillors respectively (before the method the pink
//councillor was the element 1)
assertEquals(Color.BLACK,gameBoard.getCouncillorsSet().get(1).getColor());
assertEquals(Color.PINK,gameBoard.getCouncillorsSet().get(0).getColor());
}
这是使用整数而不是 int 的方法版本
public class Player {
private int id;
private Integer councillorNumber;
public void electCouncillor(Region region,GameBoard gameBoard){
Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
region.getBalcony().add(councillor);
gameBoard.setCouncillor(region.getBalcony().element());
region.getBalcony().remove();
gameBoard.getCouncillorsSet().remove(councillorNumber);
}
public Integer getCouncillorNumber() {
return councillorNumber;
}
public void setCouncillorNumber(Integer councillorNumber) {
this.councillorNumber = councillorNumber;
}
}
我在 运行 相同测试时得到的错误是 expected <BLACK>, but was <PINK>
取自API,根据参数类型有不同的方法。第一个删除指定索引中的元素,而第二个删除元素本身,无论索引
remove(int index)
Removes the element at the specified position in
remove(Object o) Removes the first occurrence of
the specified element from this list, if it is present.
编辑以添加示例:
列表 = {1, 4, 5, 6, 9}
With int => remove(4) (删除索引为4的数字)
结果:列表 = {1, 4, 5, 6}
With Integer => remove(4)(移除值为4的元素)
结果:列表 = {1, 5, 6, 9}
我有方法 electCouncillor
从数组 CouncillorsSet
中取出某个议员,将其放入某个 balcony
队列,从中获取额外的议员 balcony
并将其放回 CouncillorSet
,以确定将从数组中取出哪个议员 我有私有 int 字段 CouncillorNumber
,然后我意识到我不能使用 int,因为每次我都使用该方法我必须重置 CouncillorNumber
值并且 int 不允许 null(如果我输入 0,它将始终使用数组中的第一个议员),所以我决定将其更改为整数,但出于某种原因测试失败了,我不知道为什么。
public class Player {
private int id;
private int councillorNumber;
public void electCouncillor(Region region,GameBoard gameBoard){
//Gets a certain councillor from the array CouncillorSet
Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
//Adds that councillor to the queue Balcony
region.getBalcony().add(councillor);
//Sets the councillor on top of the Balcony in the CouncillorSet
gameBoard.setCouncillor(region.getBalcony().element());
//Removes the councillor on top of the balcony
region.getBalcony().remove();
//Removes the councillor from CouncillorSet that was added to the balcony
gameBoard.getCouncillorsSet().remove(councillorNumber);
}
public int getCouncillorNumber() {
return councillorNumber;
}
public void setCouncillorNumber(int councillorNumber) {
this.councillorNumber = councillorNumber;
}
}
这是测试
@Test
public void testElectCouncillor(){
Player player = new Player(1);
GameBoard gameBoard = new GameBoard();
//First i create 6 councillors to be used in the test
Councillor councillor1 = new Councillor();
Councillor councillor2 = new Councillor();
Councillor councillor3 = new Councillor();
Councillor councillor4 = new Councillor();
Councillor councillor5 = new Councillor();
Councillor councillor6 = new Councillor();
councillor1.setColor(Color.BLACK);
councillor2.setColor(Color.BLUE);
councillor3.setColor(Color.ORANGE);
councillor4.setColor(Color.PURPLE);
councillor5.setColor(Color.WHITE);
councillor6.setColor(Color.PINK);
//Then i add the first 4 councillors to a balcony
Region region = gameBoard.getRegionCoast();
Queue<Councillor> balcony = region.getBalcony();
balcony.add(councillor1);
balcony.add(councillor2);
balcony.add(councillor3);
balcony.add(councillor4);
//The i add the remaining two to the CouncillorSet of the GameBoard
gameBoard.setCouncillorSet(new ArrayList<Councillor>());
gameBoard.getCouncillorsSet().add(councillor5);
gameBoard.getCouncillorsSet().add(councillor6);
//This gets the first element from CouncillorSet, which in this case is the councillor5 color white
player.setCouncillorNumber(0);
player.electCouncillor(region,gameBoard);
//After doing the method first i verify that the element in top of the queue is
//now the councillor Blue
assertEquals(Color.BLUE,region.getBalcony().element().getColor());
//Then i verify that the elements 0 and 1 from the CouncillorSet are the
//Pink and Black councillors respectively (before the method the pink
//councillor was the element 1)
assertEquals(Color.BLACK,gameBoard.getCouncillorsSet().get(1).getColor());
assertEquals(Color.PINK,gameBoard.getCouncillorsSet().get(0).getColor());
}
这是使用整数而不是 int 的方法版本
public class Player {
private int id;
private Integer councillorNumber;
public void electCouncillor(Region region,GameBoard gameBoard){
Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
region.getBalcony().add(councillor);
gameBoard.setCouncillor(region.getBalcony().element());
region.getBalcony().remove();
gameBoard.getCouncillorsSet().remove(councillorNumber);
}
public Integer getCouncillorNumber() {
return councillorNumber;
}
public void setCouncillorNumber(Integer councillorNumber) {
this.councillorNumber = councillorNumber;
}
}
我在 运行 相同测试时得到的错误是 expected <BLACK>, but was <PINK>
取自API,根据参数类型有不同的方法。第一个删除指定索引中的元素,而第二个删除元素本身,无论索引
remove(int index) Removes the element at the specified position in
remove(Object o) Removes the first occurrence of the specified element from this list, if it is present.
编辑以添加示例:
列表 = {1, 4, 5, 6, 9}
With int => remove(4) (删除索引为4的数字) 结果:列表 = {1, 4, 5, 6}
With Integer => remove(4)(移除值为4的元素) 结果:列表 = {1, 5, 6, 9}