我有一个运行良好的 spring boot 2.0.0 M2 应用程序。
我在构造函数上使用自动装配
@RequestMapping(value = "/rest")
@RestController
public class AddressRestController extends BaseController{
private final AddressService AddressService;
@Autowired
public AddressRestController(final AddressService AddressService) {
this.AddressService = AddressService;
}
...
}
@Service
public class AddressServiceImpl extends BaseService implements AddressService {
@Autowired
public AddressServiceImpl(final AddressRepository AddressRepository) {
this.AddressRepository = AddressRepository;
}
private final AddressRepository AddressRepository;
...
}
public interface AddressRepository extends JpaRepository<Address, Integer>, AddressRepositoryCustom {
}
@Repository
public class AddressRepositoryImpl extends SimpleJpaRepository implements AddressRepositoryCustom {
@PersistenceContext
private EntityManager em;
@Autowired
public AddressRepositoryImpl(EntityManager em) {
super(Address.class, em);
}
...
}
当我尝试运行基本测试时
@RunWith(SpringJUnit4ClassRunner.class)
public class AddressServiceTest {
@Autowired
private AddressService service;
@MockBean
private AddressRepository restTemplate;
@Test
public void getAddress(){
MockitoAnnotations.initMocks(this);
Pageable page = PageRequest.of(0, 20);
Page<Address> pageAdr = mock(Page.class);
given(this.restTemplate.findAll(page)).willReturn(pageAdr);
Page<AddressDto> pageDto = service.getAddress(page);
}
}
我收到这个错误
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“com.sonos.arcor.service.AddressServiceTest”的bean时出错:通过字段“服务”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.sonos.arcor.service.AddressService”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我不明白为什么我会收到这个错误。