changed userserviceimpl to constructor injection. this broke the pathetic excuse of a unit test I had made. no regrets

This commit is contained in:
nova 2019-12-10 20:13:31 +05:30
parent 849a5133d7
commit 2e7f6cced6
3 changed files with 28 additions and 20 deletions

View File

@ -2,10 +2,11 @@ package org.ros.chatto.repository;
import org.ros.chatto.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface RoleRepository extends JpaRepository<Role, Long>{
// @Query("select r from RoleRepository where name = ?1")
@Query("select r from Role r where r.name = ?1")
public Role findByName(String roleName);
}

View File

@ -13,6 +13,7 @@ import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.Role;
import org.ros.chatto.model.UserRole;
import org.ros.chatto.model.UserSession;
import org.ros.chatto.repository.RoleRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserRoleRepository;
import org.ros.chatto.repository.UserSessionRepository;
@ -24,20 +25,25 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
private final UserRepository userRepository;
@Autowired
private UserRoleRepository userRoleRepository;
private final UserRoleRepository userRoleRepository;
@Autowired
private PasswordEncoder passwordEncoder;
private final PasswordEncoder passwordEncoder;
@Autowired
private RoleService roleService;
private final RoleRepository roleRepository;
@Autowired
private UserSessionRepository userSessionRepository;
private final UserSessionRepository userSessionRepository;
public UserServiceImpl(@Autowired UserRepository userRepository, @Autowired UserRoleRepository userRoleRepository,
@Autowired PasswordEncoder passwordEncoder, @Autowired RoleRepository roleRepository,
@Autowired UserSessionRepository userSessionRepository) {
this.userRepository = userRepository;
this.userRoleRepository = userRoleRepository;
this.passwordEncoder = passwordEncoder;
this.roleRepository = roleRepository;
this.userSessionRepository = userSessionRepository;
}
@Override
public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) {
@ -46,11 +52,9 @@ public class UserServiceImpl implements UserService {
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
ChatUser changedUser = userRepository.save(user);
UserRole userRole = new UserRole();
Role role = roleService.getRole("USER");
Role role = roleRepository.findByName("USER");
userRole.setRole(role);
userRole.setUser(changedUser);
// System.out.println(role.getRoleID());
// System.out.println(changedUser.getUserID());
userRoleRepository.save(userRole);
return userRole;
}

View File

@ -15,11 +15,11 @@ import org.ros.chatto.dto.UserRegistrationDTO;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.Role;
import org.ros.chatto.model.UserRole;
import org.ros.chatto.repository.RoleRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserRepositoryCustom;
import org.ros.chatto.repository.UserRoleRepository;
import org.ros.chatto.repository.UserSessionRepository;
import org.ros.chatto.service.RoleService;
import org.ros.chatto.service.UserService;
import org.ros.chatto.service.UserServiceImpl;
import org.springframework.boot.test.context.SpringBootTest;
@ -33,9 +33,9 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class UnitTest {
@InjectMocks
private UserService userService = new UserServiceImpl();
// private RoleService roleService;
// private UserTokenService userTokenService;
// private RoleService roleService;
// private UserTokenService userTokenService;
@Mock
private UserRoleRepository userRoleRepository;
@ -44,7 +44,7 @@ public class UnitTest {
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
@Mock
private RoleService roleService;
private RoleRepository roleRepository;
@Mock
private UserRepositoryCustom userRepositoryCustom;
@ -54,6 +54,9 @@ public class UnitTest {
@Mock
private UserRepository userRepository;
private UserService userService = new UserServiceImpl(userRepository, userRoleRepository, passwordEncoder, roleRepository,
userSessionRepository);
// private ChatUser chatUser;
@Before
@ -75,7 +78,7 @@ public class UnitTest {
Role role = new Role();
role.setRoleID(2);
role.setName("USER");
when(roleService.getRole("USER")).thenReturn(role);
when(roleRepository.findByName("USER")).thenReturn(role);
when(userRepository.save(chatUser)).thenReturn(chatUser);
UserRole userRole = userService.registerUser(userRegistrationDTO);