我有一個運行良好的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 : 創建名爲 的bean的錯誤'com.sonos.arcor.service.Addre ssServiceTest':通過字段'service'表示的不滿意的依賴關係 ;嵌套異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 「'com.sonos.arcor.service.AddressService''類型的合格bean可用:預計至少有1個bean符合自動連線 候選。依賴註釋:
我不明白爲什麼我得到這個錯誤{@ org.springframework.beans.factory.annotation.Autowired(必需=真)}。