姬長信(Redy)

java – Spring Boot:当自动装配具体类时,“…


我正在使用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,因为它是我想要测试的类.我怎么做到这一点?