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

説明
com.bonansa.service.AddressServiceのコンストラクターのパラメーター2に、タイプ 'org.springframework.web.client.RestTemplate'のBeanが必要でしたが見つかりませんでした。

アクション
構成でタイプ 'org.springframework.web.client.RestTemplate'のBeanを定義することを検討してください。

回答

mare Dec 04 2020 at 04:52

SpringBootはRestTemplateを自動的に構成しません。したがって、RestTemplateを定義せずに自動配線することはできません。見るhttps://stackoverflow.com/a/42618428/1992820