mardi 3 décembre 2019

how does this junit test pass?

I am writing junit test for my service layer and got confusion while doing so. This is my service layer to save the user.

       @Override
        public UserDto createUser(UserDto user) {
            if (userRepository.findByEmail(user.getEmail()) != null) throw new RuntimeException("Email already exists!!!");
            for (int i = 0; i < user.getAddresses().size(); i++) {
                AddressDto addressDto = user.getAddresses().get(i);
                addressDto.setUserDetails(user);
                addressDto.setAddressId(utils.generateAddressId(10));
                user.getAddresses().set(i, addressDto);
            }

            UserEntity userEntity = new UserEntity();
    //        BeanUtils.copyProperties(user, userEntity);
            ModelMapper modelMapper = new ModelMapper();
            userEntity = modelMapper.map(user, UserEntity.class);

            userEntity.setEncryptedPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            String publicUserId = utils.generateUserId(30);
            userEntity.setUserId(publicUserId);

            userEntity.setEmailVerificationToken(utils.generateEmailVerificationToken(publicUserId));
            UserEntity storedUserDetails = userRepository.save(userEntity);
            UserDto returnValue = new UserDto();
            returnValue = modelMapper.map(storedUserDetails, UserDto.class);

            //Send an email for verification

            new AmazonSES().verifyEmail(returnValue);
    //        BeanUtils.copyProperties(storedUserDetails, returnValue);
            return returnValue;
        }


**And this is my test case:**

 @Test
    final void testCreateUser(){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(userId);
        userEntity.setFirstName("johan");
        userEntity.setLastName("paul");
        userEntity.setEmail("test@gmail.com");
        userEntity.setEmailVerificationToken("adsf23434q3adfadf");

        when(userRepository.findByEmail(anyString())).thenReturn(null);
        when(utils.generateAddressId(anyInt())).thenReturn("jhjh2121jhjh12");
        when(bCryptPasswordEncoder.encode(anyString())).thenReturn(encryptedPassword);
        when(utils.generateUserId(anyInt())).thenReturn(userId);
        when(utils.generateEmailVerificationToken(anyString())).thenReturn("adsfasdfasdfaew33434");
        when(userRepository.save(any(UserEntity.class))).thenReturn(userEntity);

        UserDto userDto = new UserDto();
        userDto.setAddresses(getAddressDto());
        userDto.setFirstName("jessica");
        userDto.setLastName("doe");
        userDto.setEmail("jesi@gmail.com");
        userDto.setPassword("asdfasdfasdf23");
        UserDto storedUserDetails = userService.createUser(userDto);

        assertEquals(userEntity.getFirstName(),storedUserDetails.getFirstName());
    }

The part where i got confused is how does assertEquals return true since the userEntity firstName was set to "johan" and the storedUserDetails firstName was set to "jessica"?

Aucun commentaire:

Enregistrer un commentaire