SpringBoot - Servicios de prueba [duplicado]

Dec 04 2020

Tengo una aplicación SpringBoot. con este servicio:

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

He creado esta prueba:

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

    @Autowired
    AddressService addressService;

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

Pero cuando ejecuto la prueba, tengo este error:

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

Descripción : El
parámetro 2 del constructor en com.bonansa.service.AddressService requería un bean de tipo 'org.springframework.web.client.RestTemplate' que no se pudo encontrar.

Acción :
considere la posibilidad de definir un bean de tipo 'org.springframework.web.client.RestTemplate' en su configuración.

Respuestas

mare Dec 04 2020 at 04:52

Spring Boot no configura automáticamente una RestTemplate. Por lo tanto, no puede conectar automáticamente una RestTemplate sin definir una. Verhttps://stackoverflow.com/a/42618428/1992820