SpringBoot - Testdienste [Duplikat]

Dec 04 2020

Ich habe eine SpringBoot-App. mit diesem 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) {
        // (..)
    }
}

Ich habe diesen Test erstellt:

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

    @Autowired
    AddressService addressService;

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

Aber wenn ich den Test durchführe, habe ich diesen Fehler:

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

Beschreibung :
Parameter 2 des Konstruktors in com.bonansa.service.AddressService erforderte eine Bean vom Typ 'org.springframework.web.client.RestTemplate', die nicht gefunden werden konnte.

Aktion :
Definieren Sie in Ihrer Konfiguration eine Bean vom Typ 'org.springframework.web.client.RestTemplate'.

Antworten

mare Dec 04 2020 at 04:52

Spring Boot konfiguriert eine RestTemplate nicht automatisch. Sie können eine RestTemplate also nicht automatisch verdrahten, ohne eine zu definieren. Sehenhttps://stackoverflow.com/a/42618428/1992820