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

标签 java spring-boot testing integration-testing

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

测试模块 -

  @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())来确认它们的十六进制代码可能不同。

关于java - 集成测试期间对象中的 ArrayLIst 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61556481/

相关文章:

java - 如何在 Spring boot 中检查嵌套对象来获取对象?

unit-testing - 如何让 Selenium 服务器与 htmlunit 一起工作?

testing - 似乎 Groovy 在这两种情况下的行为不同?

java - DynamoDB 在 map 中存储/映射复杂对象

java - hibernate 在父类(super class)中选择一个ID,而不为父类(super class)创建表

Java CsvToBean.parse 失败并出现错误解析 CSV

java - 是否可以在 Spring Repository @Query 注释中动态设置模式?

JAVAFX按钮 Action 调用其他类的函数

java - Spring Boot graphql 的问题。请求/graphql 结果为 404

xcode - 在 Xcode 中将 aps-environment 授权添加到 UITest 目标