1

I have an SpringBoot app. with this service:

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

I have created this test:

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

    @Autowired
    AddressService addressService;

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

But when I run the test I have this error:

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

Description:
Parameter 2 of constructor in com.bonansa.service.AddressService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

Miron Balcerzak
  • 886
  • 10
  • 21
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301

1 Answers1

0

Spring Boot does not automatically configure a RestTemplate. So you cannot autowire a RestTemplate without defining one. See https://stackoverflow.com/a/42618428/1992820

mare
  • 402
  • 6
  • 14