SpringBoot - usługi testowe [duplikat]

Dec 04 2020

Mam aplikację SpringBoot. z tą usługą:

@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) {
        // (..)
    }
}

Stworzyłem ten test:

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

    @Autowired
    AddressService addressService;

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

Ale kiedy uruchamiam test mam ten błąd:

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

Opis :
parametr 2 konstruktora w com.bonansa.service.AddressService wymagał komponentu bean typu „org.springframework.web.client.RestTemplate”, którego nie można znaleźć.

Akcja :
rozważ zdefiniowanie komponentu bean typu „org.springframework.web.client.RestTemplate” w swojej konfiguracji.

Odpowiedzi

mare Dec 04 2020 at 04:52

Spring Boot nie konfiguruje automatycznie RestTemplate. Nie możesz więc automatycznie podłączyć RestTemplate bez zdefiniowania go. Widziećhttps://stackoverflow.com/a/42618428/1992820