junit 测试功能

wvt8vs2t  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(118)

我在一个 TableService 类和一个函数中有以下变量,该函数接受列表上的currentPlayer索引,加上+1,并使currentPlayer成为具有此递增索引的那个。currentPlayer 在前面的不同方法中被赋值为currentPlayer = Optional.of(players.get(0));

private ArrayList<Player> players = new ArrayList<>();
private Optional<Player> currentPlayer = Optional.empty();        

public void setNextPlayerCurrent() {
    int currentPlayerIndex = players.indexOf(currentPlayer.get());
    if (!lastPlayerCheck(currentPlayerIndex)) {
        currentPlayer = Optional.of(players.get(currentPlayerIndex + 1));
    }
    else {
        currentPlayer = Optional.of(players.get(0));
    }
}

我想测试一下,我写了下面的函数:

@Test
void checkCurrentPlayerSetting() {
    List<Player> players = tableService.getPlayers();
    Optional<Player> currentPlayer = tableService.getCurrentPlayer();
    Player player1 = new Player("A", "Adam", 100);
    Player player2 = new Player("B", "Bella", 100);
    players.add(player1);
    players.add(player2);
    currentPlayer = Optional.of(players.get(0));

    int player1Index = players.indexOf(player1);
    int player2Index = players.indexOf(player2);
    System.out.println(player1Index); // prints 0
    System.out.println(player2Index); / prints 1
    System.out.println(players.indexOf(currentPlayer.get())); // prints 0

    tableService.setNextPlayerCurrent(); // java.util.NoSuchElementException: No value present!!!

    //assertEquals(player2.getId(), currentPlayer.get().getId());
}

我想Assert在执行 *setNextPlayerCurrent()之后 * player2将匹配currentPlayer。我应该如何处理这个异常?
也在TableService类中编辑:lastPlayerCheck():

public boolean lastPlayerCheck(int currentPlayerIndex) {
    if (currentPlayerIndex == (players.size() - 1)) {
        return true;
    }
    return false;
}
noj0wjuj

noj0wjuj1#

据我所知,TableService中的currentPlayer字段除了用Optional.empty()初始化之外,从未被赋予过其他值。因此,您的TableService可能需要一个类似setCurrentPlayer(Player player)的方法,以便您可以手动配置currentPlayer字段。注意:您尝试在测试中为currentPlayer字段赋值的方法不起作用!

// here you only get a reference to the currently empty Optional object
Optional<Player> currentPlayer = tableService.getCurrentPlayer();
// here you just assign the variable (which is only visible in the context of this test)
// to a fresh Optional holding an actual player object - this has no effect on the
// currentPlayer field inside the TableService 
currentPlayer = Optional.of(players.get(0));

也许你的下一个想法是做这样的事情:

Optional<Player> currentPlayer = tableService.getCurrentPlayer();
currentPlayer.setValue(players.get(0)) // WILL NOT COMPILE

但是Java Optionals是IMMUTABLE的,这意味着你不能给一个Optional Object赋值,而是必须创建一个新的Optional对象,并将其赋值给这个对象,这就是为什么Optional类中没有setValue()这样的方法。

**更新:**我认为您还没有完全理解method parameters的工作原理。您基本上要做的是:

void updateString(String: someString){
  someString = "updated string value"
}

String someString = "initial string value"
updateString(someString)
println(someString) // => will still be "initial string value"

当您将变量作为参数传递给方法时,您不能在该方法的上下文中重新分配该变量,因此方法外的变量(从调用此方法的位置)也被重新赋值!实际上,updateString(String: someString)方法中的someString参数与外部作用域中的someString变量是不同的变量(从调用方法的地方),但只保存对相同值的引用(在本例中为“更新的字符串值”)。因此,当您在内部方法中重新分配该变量时,并不是在更新已传递到方法中的变量(在外部作用域中)!

相关问题