SpringBoot - Servizi di test [duplicato]
Dec 04 2020
Ho un'app SpringBoot. con questo servizio:
@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) {
// (..)
}
}
Ho creato questo test:
@ExtendWith(SpringExtension.class)
@SpringBootTest
class AddressServiceTest {
@Autowired
AddressService addressService;
@Test
void searchFromAddress() {
System.out.println
(addressService.searchFromAddress("Plaza los Cubos)"));
}
}
Ma quando eseguo il test ho questo errore:
***************************
APPLICATION FAILED TO START
***************************
Descrizione : il
parametro 2 del costruttore in com.bonansa.service.AddressService richiedeva un bean di tipo "org.springframework.web.client.RestTemplate" che non è stato trovato.
Azione :
prendere in considerazione la definizione di un bean di tipo "org.springframework.web.client.RestTemplate" nella configurazione.
Risposte
mare Dec 04 2020 at 04:52
Spring Boot non configura automaticamente un RestTemplate. Quindi non puoi autowire un RestTemplate senza definirne uno. Vederehttps://stackoverflow.com/a/42618428/1992820