首页 > 解决方案 > 集成测试期间对象中的 ArrayLIst 未更新

问题描述

我有一个服务层类,它接受一个 userId 并将 userId 添加到 Lobby 对象中的一个数组列表中。我需要为此功能执行集成测试。但不知何故,代码没有将更新的数组列表返回给测试类。在服务层它工作正常,但是当断言 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

我无法解决这个问题。如果对象有一些参考问题?

标签: javaspring-boottestingintegration-testing

解决方案


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

Lobby lobby = getLobby(id);

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


推荐阅读