Injection injection not working in spring boot test but working normally
I started developing a classic Spring Boot MVC application. I am using dependency injection (using annotation @Service
, @Autowired
) without any problem.
When I try to run some integration test using Dependency Injection, I get the following error from Junit:
org.springframework.beans.factory.UnsatisfiedDependencyException: An error occurred while creating a bean named "hu.bookandwalk.RepositoryTests": Insufficient dependency expressed through userService field; nested exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hu.bookandwalk.services.UserService' available: At least 1 bean expected, which qualifies as a self-timer candidate. Dependency annotations: {@ Org.springframework.beans.factory.annotation.Autowired (required = true)}
Relevant part of the test code:
package hu.bookandwalk;
...
import hu.bookandwalk.config.MyNeo4jConfig;
import hu.bookandwalk.domain.*;
import hu.bookandwalk.services.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
@ContextConfiguration(classes={MyNeo4jConfig.class})
public class RepositoryTests {
@Autowired
Session session;
@Autowired
UserService userService;
...
}
In a package hu.bookandwalk.services
, I have a UserService
non-annotated interface and a class UserServiceImpl
annotated with @Service
.
I don't understand what if DI is working to run my application and not why it is not working in test. Somehow my annotated implementation class is not showing up on Spring Boot as error messages are reporting.
The test is in the same package as my class applications: hu.bookandwalk
All my life, the repository domains are located under this: hu.bookandwalk.services
, hu.bookandwalk.domain
...
Any idea what annotation I missed for the test class to make it UserServiceImpl
discoverable?
source to share