SpringBoot-테스트 서비스 [중복]

Dec 04 2020

SpringBoot 앱이 있습니다. 이 서비스로 :

@Slf4j
@Service
public class AddressService {

    private final RegionRepository regionRepository;
    private final CommuneRepository communeRepository;
    private final RestTemplate restTemplate;

    public AddressService(RegionRepository regionRepository,
                          CommuneRepository communeRepository,
                          RestTemplate restTemplate) {
        this.regionRepository = regionRepository;
        this.communeRepository = communeRepository;
        this.restTemplate = restTemplate;
    }

    public GeolocationAddress searchFromAddress(String address) {
        // (..)
    }
}

이 테스트를 만들었습니다.

@ExtendWith(SpringExtension.class)
@SpringBootTest
class AddressServiceTest {

    @Autowired
    AddressService addressService;

    @Test
    void searchFromAddress() {
        System.out.println
                (addressService.searchFromAddress("Plaza los Cubos)"));
    }
}

하지만 테스트를 실행하면이 오류가 발생합니다.

***************************
APPLICATION FAILED TO START
***************************

설명 :
com.bonansa.service.AddressService에서 생성자의 매개 변수 2에는 찾을 수없는 'org.springframework.web.client.RestTemplate'유형의 Bean이 필요합니다.

조치 :
구성에서 'org.springframework.web.client.RestTemplate'유형의 Bean을 정의하는 것을 고려하십시오.

답변

mare Dec 04 2020 at 04:52

Spring Boot는 RestTemplate을 자동으로 구성하지 않습니다. 따라서 RestTemplate을 정의하지 않고는 Autowire를 사용할 수 없습니다. 보다https://stackoverflow.com/a/42618428/1992820