Spring Boot - 测试服务
SpringBoot - Testing services
我有一个 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。
Spring 开机不会自动配置一个RestTemplate。因此,您不能在不定义 RestTemplate 的情况下自动装配 RestTemplate。参见
我有一个 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。
Spring 开机不会自动配置一个RestTemplate。因此,您不能在不定义 RestTemplate 的情况下自动装配 RestTemplate。参见