SpringBoot - Dịch vụ kiểm tra [trùng lặp]

Dec 04 2020

Tôi có một ứng dụng SpringBoot. với dịch vụ này:

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

Tôi đã tạo bài kiểm tra này:

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

    @Autowired
    AddressService addressService;

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

Nhưng khi tôi chạy thử nghiệm, tôi gặp lỗi này:

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

Mô tả :
Không tìm thấy tham số 2 của hàm tạo trong com.bonansa.service.AddressService yêu cầu một bean kiểu 'org.springframework.web.client.RestTemplate'.

Hành động :
Xem xét việc xác định bean thuộc loại 'org.springframework.web.client.RestTemplate' trong cấu hình của bạn.

Trả lời

mare Dec 04 2020 at 04:52

Spring Boot không tự động cấu hình RestTemplate. Vì vậy, bạn không thể tự động tạo một RestTemplate mà không xác định một. Xemhttps://stackoverflow.com/a/42618428/1992820