Kotlin Spring引导:无法对依赖于@Qualifier注释的服务的控制器进行junit测试
我们有两个接口的实现类,我使用@qualifier来选择其中一个实现,它工作得很好。但是在kotlin junit测试中,我在junit测试尝试在controller中创建EmailService的bean时出错。当qualifier明确提到它时,它无法解决要使用哪个实现我们使用的是jupiter junit,这是kotlin测试类Kotlin Spring引导:无法对依赖于@Qualifier注释的服务的控制器进行junit测试,kotlin,junit,qualifiers,Kotlin,Junit,Qualifiers,我们有两个接口的实现类,我使用@qualifier来选择其中一个实现,它工作得很好。但是在kotlin junit测试中,我在junit测试尝试在controller中创建EmailService的bean时出错。当qualifier明确提到它时,它无法解决要使用哪个实现我们使用的是jupiter junit,这是kotlin测试类 controller class: @RestController public class ProfileController { //injecti
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 while running the test:
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)
}