我正在使用Spring Boot和Spring Boot JPA编写一个组件.我有这样的设置:
界面:
public interface Something {
// method deFinitions
}
实施:
@Component
public class SomethingImpl implements Something {
// implementation
}
现在,我有一个使用SpringJUnit4ClassRunner运行的JUnit测试,我想用它来测试我的SomethingImpl.
当我做
@Autowired
private Something _something;
它有效,但是
@Autowired
private SomethingImpl _something;
导致测试失败抛出NoSuchBeanDeFinitionException并且消息没有找到类型为[com.example.somethingImpl]的限定bean依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
但是在测试用例中,我想明确地注入我的SomethingImpl,因为它是我想要测试的类.我怎么做到这一点?
最佳答案
如果你想要一个特殊的bean,你必须使用@Qualifier注释:
@Autowired
@Qualifier("SomethingImpl")
private Something _something;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。