У меня есть реализация интерфейса service
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public User findUserByEmail(String email) {
return userRepository.findByEmail(email);
}
@Override
public void saveUser(User user) {
List<User> list=userRepository.findAll();
Role userRole;
if(list.size()==0){
userRole = roleRepository.findByRole("ADMIN");
}
else {
userRole = roleRepository.findByRole("USER");
}
user.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
user.setActive(1);
userRepository.save(user);
}
}
У своем тесте я пытаюсь сохранить пользователя
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(value = false)
public class UserServiceTests {
@Autowired
private UserRepository userRepository;
@Autowired(required=true)
private UserServiceImpl userService;
@Test
public void saveUserTest(){
userService.saveUser(getUser());
User userAct=userRepository.findById(53);
assertEquals("yakov83@mail.com",userAct.getEmail());
}
private User getUser() {
User user = new User();
user.setFirstname("Маргарита");
user.setLastname("Яковлева");
user.setPatronymic("Ивановна");
user.setEmail("yakov83@mail.com");
user.setPassword("UsRe107066");
user.setPhone("+375449873480");
return user;
}
}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.zaitsava.springboot_touristsite.UserServiceTests': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zaitsava.springboot_touristsite.service.UserServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zaitsava.springboot_touristsite.service.UserServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
я так понимаю ему что-то не нравится UserServiceImpl, но не совсем понимаю что. Или ему обязательно нужно Mock использовать? По возможности хотелось бы без него обойтись.