在集成测试期间,对象中的ArrayList未进行更新。

huangapple go评论56阅读模式
英文:

ArrayLIst in an object is not updating during Integration test

问题

以下是翻译好的部分:

我有一个服务层类,它接收一个 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("游戏正在进行中。您不能在游戏进行中加入大厅。请稍后再试。");
}

//在将用户添加到大厅之前检查用户是否存在
try {
    userRepository.findById(userId);
} catch (Exception e) {
    throw new LobbyException(String.format("ID 为 %d 的用户不存在", userId));
}
String baseErrorMessage = "大厅不能超过7名玩家。请加入其他大厅。";

System.out.println("service2->" + lobby.getId() + " " + lobby.getPlayerIds().size());
//大厅的大小限制为最多7名玩家。
if(lobby.getPlayerIds().size() > 7){
    throw new LobbyException(baseErrorMessage);
}

//大厅中的玩家应该是唯一的
if(lobby.getPlayerIds().contains(userId)){
    baseErrorMessage = "玩家已存在于大厅中";
    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 optionalLobby = lobbyRepository.findById(id);
if (!optionalLobby.isPresent()) {
throw new LobbyException(String.format("找不到 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

我无法找到问题所在。是否与对象存在某种引用问题有关?

(以上内容为翻译结果,只包含翻译后的部分,不包含其他内容。)

英文:

I have one service layer class which takes in a userId and add the userId to an arraylist in the Lobby object.I need to perform Integration test for this functionality. But somehow the code is not returning the updated arraylist to the test class. In service layer its working fine but when asserting the arraylist updated size, its failing.

Test module -

  @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);
}

Service method which I need to test

    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());
}

For my testing I have put some print statement which clearly shows that its updating the arraylist in service layer but its not replicated in integration test method.

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

I am unable to get the issue. If there is some reference issue with the object?

答案1

得分: 0

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

Lobby lobby = getLobby(id);

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

英文:

I'd need to see the full source code to be sure, but it seems the Lobby object returned at:

Lobby lobby = getLobby(id);

is not the same object as the Lobby you set up in your test.
You can confirm that by checking the object signature (toString()), their Hex codes are likely different.

huangapple
  • 本文由 发表于 2020年5月2日 16:29:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61556481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定