首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Junit配置类未加载

Spring Junit配置类未加载
EN

Stack Overflow用户
提问于 2021-06-11 18:30:14
回答 2查看 123关注 0票数 0

我正在编写一个junit测试,其中我需要自动连接接口的特定实现。我使用@Mock注解来自动连接实现。我正在使用配置文件和一个配置文件来确定哪个实现是Autowire。运行测试类EmailTest时,控制台上会出现以下错误消息:

原因: java.lang.IllegalStateException:无法注册模拟bean ....需要一个要替换的匹配bean,但找到了customerEmailSender、emailSenderImpl_1、emailSenderImpl_2

原因是Spring没有找到或使用配置类: BeanConfiguration。我之所以知道这一点,是因为我在BeanConfiguration类中设置了断点,而应用程序不会中断。

Spring没有发现或使用配置类BeanConfiguration的原因可能是什么。

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = {"test-unit"})
@Import(BeanConfiguration.class)
public class EmailTest {

    @MockBean
    private CustomerEmailSender customerEmailSender;
    
}



@Configuration
public class BeanConfiguration {


    @Profile({"test-unit"})
    @Bean(name = "customerEmailSender")
    public CustomerEmailSender emailSenderImpl_1(){
        return new EmailSenderImpl_1();
    }

    @Profile({"prd"})
    @Bean(name = "customerEmailSender")
    public CustomerEmailSender emailSenderImpl_2(){
        return new EmailSenderImpl_2();
    }

}
EN

回答 2

Stack Overflow用户

发布于 2021-06-11 18:38:29

检查BeanConfiguration包是否在spring的组件扫描边界内

票数 0
EN

Stack Overflow用户

发布于 2021-06-11 18:41:46

问题是@MockBean不知道要替换哪个bean (并且您有多个相同接口的bean)。

试一试

代码语言:javascript
复制
@MockBean(name = "customerEmailSender")
private CustomerEmailSender customerEmailSender;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67935414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档