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
***************************

Описание .
Параметр 2 конструктора в com.bonansa.service.AddressService требовал bean-объекта типа org.springframework.web.client.RestTemplate, который не удалось найти.

Действие :
Рассмотрите возможность определения bean-компонента типа org.springframework.web.client.RestTemplate в вашей конфигурации.

Ответы

mare Dec 04 2020 at 04:52

Spring Boot не настраивает RestTemplate автоматически. Таким образом, вы не можете автоматически подключить RestTemplate, не определив его. Увидетьhttps://stackoverflow.com/a/42618428/1992820