Reputation: 12081
This has been answered several times but I have all the annotations set and it appears to be wired correctly.
UserService interface:
public interface UserService {
User findUserById(Long id);
User findUserByName(String name);
List<User> findAllUsers();
void saveUser(User user);
void updateUser(User user);
void deleteUserById(long id);
void deleteAllUsers();
public boolean isUserExist(User user);
}
UserServiceImpl
@Service
@Transactional
public class UserServiceImpl implements UserService {
private static final AtomicLong counter = new AtomicLong();
private static List<User> users;
static {
users = populateDummyUsers();
}
@Override
public User findUserById(Long id) {
for (User user : users) {
if (user.getId() == id) {
return user;
}
}
return null;
}
@Override
public User findUserByName(String name) {
for (User user : users) {
if (user.getUsername().equalsIgnoreCase(name)) {
return user;
}
}
return null;
}
Test case which is failing:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringCrudApplication.class)
@ComponentScan("com.service")
public class ServiceTest {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@Test
public void findUserByIdTest() throws Exception {
long id = (long) 0;
User userTest = new User(id,"Mike","644 Main St", "[email protected]");
User user = userService.findUserById(id);
assert user.getAddress() != null;
assert user.getEmail() != null;
assert user.getUsername() != null;
}
I have tried to use componentScan in the test class on com.service and com.serviceImpl with no luck.
Full error:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'com.service.test.ServiceTest':
Unsatisfied dependency expressed through method 'setUserService'
parameter 0: No qualifying bean of type [com.service.UserService]
found for dependency [com.service.UserService]: expected at least 1
bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {}; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [com.service.UserService] found for
dependency [com.service.UserService]: expected at least 1 bean which
qualifies as autowire candidate for this dependency. Dependency
annotations: {}
-------------------SOLUTION-------------------
I had to add @ComponentScan to the main method:
@SpringBootApplication
@ComponentScan("com.serviceImpl, com.service")
public class SpringCrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCrudApplication.class, args);
}
}
Upvotes: 2
Views: 1479
Reputation: 9516
The implementation must be found by ComnponentScan, not the interface. So if your class UserServiceImpl is in package com.serviceImpl this package must be scanned.
The package name looks strange,package name should be lowercase only. So try renaming the package to com.service.impl and scan that.
Because you are using @Transactional, spring will create a proxy that implements UserService. So you can only get UserService injected but not UserServiceImpl. Check your code, may be you are trying to @Autowire a UserServiceImpl somewhere else.
Along with your exception 'No matching bean of type..' often there is a message like 'could not autowire ' in the stacktrace, that tells exactly, where spring tried to inject what type.
Upvotes: 2