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
***************************
คำอธิบาย :
พารามิเตอร์ 2 ของคอนสตรัคเตอร์ใน com.bonansa.service.AddressService ต้องการ bean ชนิด 'org.springframework.web.client.RestTemplate' ที่ไม่พบ
การดำเนินการ :
พิจารณากำหนดชนิดของ bean 'org.springframework.web.client.RestTemplate' ในการกำหนดค่าของคุณ
คำตอบ
mare Dec 04 2020 at 04:52
Spring Boot ไม่ได้กำหนดค่า RestTemplate โดยอัตโนมัติ ดังนั้นคุณจึงไม่สามารถกำหนด RestTemplate โดยอัตโนมัติได้โดยไม่ต้องกำหนด ดูhttps://stackoverflow.com/a/42618428/1992820