SpringBoot - serviços de teste [duplicado]

Dec 04 2020

Eu tenho um aplicativo SpringBoot. com este serviço:

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

Eu criei este teste:

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

    @Autowired
    AddressService addressService;

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

Mas quando executo o teste, recebo este erro:

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

Descrição : o
parâmetro 2 do construtor em com.bonansa.service.AddressService exigia um bean do tipo 'org.springframework.web.client.RestTemplate' que não foi encontrado.

Ação :
Considere definir um bean do tipo 'org.springframework.web.client.RestTemplate' em sua configuração.

Respostas

mare Dec 04 2020 at 04:52

Spring Boot não configura automaticamente um RestTemplate. Portanto, você não pode fazer o autowire de um RestTemplate sem definir um. Vejohttps://stackoverflow.com/a/42618428/1992820