स्प्रिंगबूट - परीक्षण सेवाएँ [डुप्लिकेट]

Dec 04 2020

मेरे पास स्प्रिंगबूट ऐप है। इस सेवा के साथ:

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

विवरण :
com.bonansa.service.AddressService में कंस्ट्रक्टर के पैरामीटर 2 के लिए ing org.springframework.web.client.RestTemplate ’प्रकार की बीन की आवश्यकता होती है जो नहीं मिल सकी।

क्रिया :
अपने विन्यास में 'org.springframework.web.client.RestTemplate' प्रकार की सेम को परिभाषित करने पर विचार करें।

जवाब

mare Dec 04 2020 at 04:52

स्प्रिंग बूट स्वचालित रूप से एक RestTemplate को कॉन्फ़िगर नहीं करता है। तो आप एक को परिभाषित किए बिना एक RestTemplate को स्वीकार नहीं कर सकते। ले देखhttps://stackoverflow.com/a/42618428/1992820