we have two implementation classes of a interface and i am using @qualifier to choose one of the implementation and it works fine.However in kotlin junit testing i am getting error while junit test trying to create bean of EmailService in controller .It is not able to resolve which implementation to use while it clearly mentioned by qualifier to use one implementation .we are using jupiter junit and this is kotlin test class.
controller class:
@RestController
public class ProfileController {
//injecting one of the implementation of EmailService
@Autowired
@Qualifier("harmonyService")
private EmailService emailService;
........
}
EmailService interface:
public interface EmailService {
void someMethod();
}
we have two implementation classes of Email Service
a) First Implementation
@Component("harmonyService")
public class EmailHarmonyServiceImpl implements EmailService {
void someMethod() {
//implementation
}
}
b)second implementation
@Component("notificationService")
public class EmailNotificationServiceImpl implements EmailService {
void someMethod() {
//implementation
}
}
junit test:
@ExtendWith(SpringExtension::class)
@ActiveProfiles("test,jsonlogs")
@WebMvcTest(value = [ProfileController::class])
class ProfileApiApplicationTests {
@Autowired
private lateinit
var emailService: EmailService
//some more code
}
Error:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'profileController': Unsatisfied dependency expressed through field 'emailService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.loyaltyone.account.profileapi.services.EmailService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=harmonyService)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
Comment From: scottfrederick
Thanks for getting in touch, but there isn't enough information provided to be able to provide much assistance. It isn't clear where the EmailService
bean should be getting created in the test setup.
It feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: cshekhar786
Thanks for getting in touch, but there isn't enough information provided to be able to provide much assistance. It isn't clear where the
EmailService
bean should be getting created in the test setup.It feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
@scottfrederick not sure if this is defect but i updated my question and provided more details.Issue is with junit testing
Comment From: wilkinsona
You are using @WebMvcTest
which limits the beans that are discovered via component scanning. Regular @Component
s are not included. As described in the documentation, you can use @Import
to register extra components.