UserService { @Transactional public User save(UserDTO userDTO) { User user = GenericConverter.mapper(userDTO, User.class); user.setType(TypeUser.DATABASE); user.setPassword(passwordEncoder.encode(user.getPassword())); user = userRepository.save(user); return user; }  @Transactional User save(UserDTO userDTO); User findByUsername(String username); User find(Long id); @Transactional(readOnly = false) UserPage list(UserDTO userDTO, PageableDTO pageableDTO); @Transactional(readOnly = false) List<User> list(UserDTO userDTO); @Transactional void delete(Long userId); @Transactional User update(Long userId, UserEditDTO userDTO); void updatePassword(Principal principal, String currentPassword, String newPassword, String confirmNewPassword);  }
@Test public void avoidPlainTextPassword() { UserDTO userDTO = new UserDTO(); userDTO.setEmail("foobar@email.com.br"); userDTO.setUserName("foobar"); userDTO.setFirstName("foo"); userDTO.setLastName("bar"); userDTO.setPassword("123456"); String hashPassword = bcrypt.encode(userDTO.getPassword()); Mockito.when(passwordEncoder.encode(Mockito.anyString())).thenReturn(hashPassword); userService.save(userDTO); assertTrue(bcrypt.matches(userDTO.getPassword(), hashPassword)); assertFalse(bcrypt.matches("654321", hashPassword)); Mockito.verify(userRepository, Mockito.times(1)).save(Mockito.any(User.class)); }