【发布时间】:2018-07-08 06:53:54
【问题描述】:
我用的是spring boot 2
我创建了一个基本测试
@RunWith(SpringJUnit4ClassRunner.class)
public class VehicleServiceImplTest {
private VehiculeServiceImpl service;
@Autowired
private VehicleRepository repository;
@Before
public void prepare() {
service = new VehiculeServiceImpl(repository);
}
@Test
public void test(){
}
}
我明白了
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建具有名称的 bean 时出错 'com.namur.service.VehicleServiceImplTest':不满足的依赖关系 通过字段“存储库”表示;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 'com.namur.repository.VehicleRepository' 类型的合格 bean 可用:预计至少有 1 个符合 autowire 条件的 bean 候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
如果我用 MockBean 替换 Autowired,它可以工作,但我不知道为什么
【问题讨论】:
标签: spring-boot junit