对象中的 ArrayLIst 在集成测试期间未更新

ArrayLIst in an object is not updating during Integration test

我有一个服务层 class,它接收一个 userId 并将该 userId 添加到大厅中的数组列表 object.I 需要为此功能执行集成测试。但是不知何故,代码没有将更新的数组列表返回给测试 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());
}

为了我的测试,我放了一些打印语句,清楚地表明它更新了服务层中的数组列表,但它没有在集成测试方法中复制。

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

我无法解决问题。对象是否存在引用问题?

我需要查看完整的源代码才能确定,但​​ Lobby 对象似乎返回于:

Lobby lobby = getLobby(id);

与您在测试中设置的大厅不是同一个对象。 您可以通过检查对象签名 (toString()) 来确认它们的十六进制代码可能不同。