samedi 2 mai 2020

ArrayLIst in an object is not updating during Integration test

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 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?

Aucun commentaire:

Enregistrer un commentaire