Java Mockito问题:mock正在调用实际方法
我在Mockito中遇到了一个问题,测试中调用的是实方法,而不是mocked方法。搜索了几个小时,但没有找到合适的答案 以下是我正在测试的服务:Java Mockito问题:mock正在调用实际方法,java,mockito,Java,Mockito,我在Mockito中遇到了一个问题,测试中调用的是实方法,而不是mocked方法。搜索了几个小时,但没有找到合适的答案 以下是我正在测试的服务: @Service public class DwpApiService { @Autowired private RestTemplate restTemplate = new RestTemplate(); @Bean public RestTemplate restTemplate() { ret
@Service
public class DwpApiService {
@Autowired
private RestTemplate restTemplate = new RestTemplate();
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
private GeoService geoService = new GeoService();
private String baseUri = "XXXX";
private double cityLatt = 51.5074;
private double cityLong = -0.1278;
public List<PersonApiModel> getAllUsersInCityOrWithinDistanceOfCity(String cityName, double distanceInMiles) throws RuntimeException {
HashMap<Integer, PersonApiModel> usersInCityOrWithinDistance = new HashMap<>();
List<PersonApiModel> usersInCity = getAllUsersInCity(cityName).getBody();
for (PersonApiModel person: usersInCity) {
usersInCityOrWithinDistance.put(person.getId(), person);
}
List<PersonApiModel> allUsers = getAllUsers().getBody();
for (PersonApiModel person : allUsers) {
boolean withinDistance = geoService.isLocationWithinDistance(distanceInMiles, cityLatt, cityLong, person);
if (withinDistance && !usersInCityOrWithinDistance.containsKey(person.getId())) {
usersInCityOrWithinDistance.put(person.getId(), person);
}
}
return new ArrayList<>(usersInCityOrWithinDistance.values());
}
public ResponseEntity<List<PersonApiModel>> getAllUsersInCity(String cityName) throws RuntimeException {
String cap = cityName.substring(0, 1).toUpperCase() + cityName.substring((1));
if (!cap.equals("London")) {
throw new IllegalArgumentException("Invalid city name. Please only use the city of London.");
}
return restTemplate.exchange(
baseUri + "city/" + cap + "/users",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<PersonApiModel>>(){}
);
}
public ResponseEntity<List<PersonApiModel>> getAllUsers() throws RuntimeException {
return restTemplate.exchange(
baseUri + "/users",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<PersonApiModel>>(){}
);
}
}
有人知道这里发生了什么吗?在这件事上花费了好几个小时,却似乎无法弄明白。原因可能是你正直接面对它
private GeoService geoService = new GeoService();
您可能需要用@Service注释类GeoService,并将其自动连接到dwservice上。可能的问题是您指定了确切的参数。考虑McCito配置作为一组规则,所以精确的值在那里不起作用(除非你真的很幸运,它只是神奇地工作于一些特定的窄的情况)。 所以试着换掉这个:
when(geoService.isLocationWithinDistance(distanceInMiles, cityLatt, cityLong, person))
.thenReturn(true);
为此:
when(geoService.isLocationWithinDistance(eq(distanceInMiles), eq(cityLatt), eq(cityLong), eq(person)))
.thenReturn(true);
此外,我有时在使用时遇到问题(…)。然后返回(…)
构造-例如,它不能与@Spy
一起正常工作。因此,我通常更喜欢这种方法:
doReturn(true).when(geoService)
.isLocationWithinDistance(eq(distanceInMiles), eq(cityLatt), eq(cityLong), eq(person));
如果某些参数对您(或所有参数)并不重要,请不要使用eq(…)
,也不要使用任何值-只需将相关参数替换为any()
附言:我只更改了一条模拟规则,假设您将自己查看其他规则。尝试自动连线
geoService
,而不是使用new
初始化。尝试了,得到了相同的错误。这是否回答了您的问题@Progman试过了,但似乎不起作用。下面的Stepio找到了一个有效的答案。不完全确定为什么!将注释@Service
添加到GeoService
类中,并将@Autowired
添加到DwiApiService
类中的GeoService
参考中。仍然收到相同的错误。Hmmm我在(geoService.isLocationWithindDistance(distanceInMiles,cityLat,cityLong,person))时替换了,然后返回(true)使用when(geoService.isLocationWithinDistance(anyDouble(),anyDouble(),anyDouble(),anyDouble(),any())进行编码>,然后返回(true)代码>成功了!不知道为什么,所以我会更深入地研究。谢谢
when(geoService.isLocationWithinDistance(distanceInMiles, cityLatt, cityLong, person))
.thenReturn(true);
when(geoService.isLocationWithinDistance(eq(distanceInMiles), eq(cityLatt), eq(cityLong), eq(person)))
.thenReturn(true);
doReturn(true).when(geoService)
.isLocationWithinDistance(eq(distanceInMiles), eq(cityLatt), eq(cityLong), eq(person));