【发布时间】:2015-05-07 20:22:53
【问题描述】:
我正在使用 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;
导致测试失败,抛出带有消息No qualifying bean of type [com.example.SomethingImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 的NoSuchBeanDefinitionException
但在测试用例中,我想显式地注入我的SomethingImpl,因为它是我要测试的类。我该如何做到这一点?
【问题讨论】:
标签: java spring hibernate spring-mvc spring-boot