簡體   English   中英

object 中的 ArrayLIst 在集成測試期間未更新

[英]ArrayLIst in an object is not updating during Integration test

我有一個服務層 class 接受用戶 ID 並將用戶 ID 添加到大廳 object 中的 arraylist。我需要對此功能執行集成測試。 但不知何故,代碼沒有將更新的 arraylist 返回到測試 class。 在服務層它工作正常,但是當斷言 arraylist 更新大小時,它失敗了。

測試模塊 -

  @BeforeEach
public void setupLobby(){

    testUser = new User();
    testUser.setPassword("testName");
    testUser.setUsername("testUsername");

    testUser = userService.createUser(testUser);

    lobbyTest = new Lobby();
    lobbyTest.setName("testLobby");
    lobbyTest.setHostPlayerId(testUser.getId());

    lobbyId = lobbyService.createLobby(lobbyTest);



}

    @Test
public void addUserToLobby(){

    System.out.println("before ->"+lobbyTest.getPlayerIds().size());
    User newUser = new User();
    newUser.setUsername("user2");
    newUser.setPassword("password");

    newUser = userService.createUser(newUser);
    System.out.println("new user ->"+newUser.getId());

    lobbyService.addPlayerToLobby(lobbyTest.getId(),newUser.getId());
    System.out.println("after->"+lobbyTest.getPlayerIds().size());
    assertEquals(lobbyTest.getPlayerIds().size(),2);
}

我需要測試的服務方法

    public void addPlayerToLobby(long id, long userId){
    Lobby lobby = getLobby(id);

    System.out.println("service ->"+lobby.getId()+" "+lobby.getPlayerIds().size());
    if(lobby.getStatus()==1){
        throw new LobbyException("Game is in progress. You can't join lobby in the middle of the game. Please try later");
    }

    //Checking if the user exists before adding the user to lobby
    try {
        userRepository.findById(userId);
    } catch (Exception e) {
        throw new LobbyException(String.format("User with id: %d doesn't exist", userId));
    }
    String baseErrorMessage = "The lobby cannot have more than 7 player. Please join different lobby";

    System.out.println("service2->"+lobby.getId()+" "+lobby.getPlayerIds().size());
    //Size of lobby is limited to maximum of 7 players.
    if(lobby.getPlayerIds().size()>7){
        throw new LobbyException(baseErrorMessage);
    }

    //Player should be unique in the lobby
    if(lobby.getPlayerIds().contains(userId)){
        baseErrorMessage = "Player already exists in the lobby";
        throw new LobbyException(baseErrorMessage);
    }
    lobby.getPlayerIds().add(userId);
    saveOrUpdate(lobby);
    System.out.println("service3->"+lobby.getId()+" "+lobby.getPlayerIds().size());
}


    public Lobby getLobby(Long id) {
    Optional<Lobby> optionalLobby = lobbyRepository.findById(id);
    if (!optionalLobby.isPresent()) {
        throw new LobbyException(String.format("Could not find lobby with id %d.", id));
    }
    return optionalLobby.get();
}

public void saveOrUpdate(Lobby updateLobby){
    lobbyRepository.save(updateLobby);
    lobbyRepository.flush();
    System.out.println("service method ->"+updateLobby.getId()+" "+updateLobby.getPlayerIds().size());
}

對於我的測試,我放了一些打印語句,清楚地表明它在服務層更新了 arraylist,但它沒有在集成測試方法中復制。

before ->1
new user ->3
service ->2 1
service2->2 1
service method ->2 2
service3->2 2
after->1

我無法解決這個問題。 如果 object 有一些參考問題?

我需要查看完整的源代碼才能確定,但大廳 object 似乎返回:

Lobby lobby = getLobby(id);

與您在測試中設置的大廳不一樣 object。 您可以通過檢查 object 簽名 (toString()) 來確認,它們的十六進制代碼可能不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM