gpt4 book ai didi

java - Mockito 问题 : mock is calling actual method

转载 作者:行者123 更新时间:2023-12-01 14:31:55 29 4
gpt4 key购买 nike

我在使用 Mockito 时遇到问题,在测试中调用的是真实方法而不是模拟方法。搜索了几个小时,但未能找到合适的答案。

这是我正在测试的服务:

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

}

此服务调用以下 GeoService 类:

public class GeoService {

//taken from https://www.movable-type.co.uk/scripts/latlong.html

public boolean isLocationWithinDistance(double radiusInMiles,
double sourceLat, double sourceLong,
PersonApiModel personApiModel) throws IllegalArgumentException {

boolean isLatLongWithinRange = checkIfLatLongAreWithinRange(
sourceLat, sourceLong,
personApiModel.getLatitude(), personApiModel.getLongitude());

if (!isLatLongWithinRange) {
throw new IllegalArgumentException("Latitude or Longitude are not valid values for id=" + personApiModel.getId());
}

int earthMeanRadius = 6371;

double latDiff = Math.toRadians(sourceLat - personApiModel.getLatitude());
double longDiff = Math.toRadians(sourceLong - personApiModel.getLongitude());
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2)
+ Math.cos(Math.toRadians(sourceLat)) * Math.cos(Math.toRadians(personApiModel.getLatitude()))
* Math.sin(longDiff / 2) * Math.sin(longDiff / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = earthMeanRadius * c;

double radiusInKm = radiusInMiles * 1.609344;

return radiusInKm >= distance;
}

public boolean checkIfLatLongAreWithinRange(double sourceLat, double sourceLong,
double destinationLat, double destinationLong) {
boolean sourceLatInRange = Math.abs(sourceLat) <= 90;
boolean destLatInRange = Math.abs(destinationLat) <= 90;
boolean sourceLongLessThanMinus180 = sourceLong >= -180;
boolean sourceLongLessThan80 = sourceLong <= 80;
boolean destLongLessThanMinus180 = destinationLong >= -180;
boolean destLongLessThan80 = destinationLong <= 80;

return sourceLatInRange && destLatInRange
&& sourceLongLessThanMinus180 && sourceLongLessThan80
&& destLongLessThanMinus180 && destLongLessThan80;
}
}

我的测试用例是这样的:

@RunWith(MockitoJUnitRunner.class)
public class DwpApiServiceTest {

@Mock
private RestTemplate restTemplate;

@Mock
private GeoService geoService;

@InjectMocks
private final DwpApiService dwpApiService = new DwpApiService();

private final PersonApiModel personApiModel1 = new PersonApiModel();
private final PersonApiModel personApiModel2 = new PersonApiModel();

private final List<PersonApiModel> fakeList1 = new ArrayList<>();
private final List<PersonApiModel> fakeList2 = new ArrayList<>();

private final String baseUri = "XXXXXX";
private double cityLatt = 51.5074;
private double cityLong = -0.1278;

@Test
public void givenMocks_whenGetAllUsersInAndAroundCity_returnMockedPersonList() {
personApiModel1.setId(1);
fakeList1.add(personApiModel1);
ResponseEntity<List<PersonApiModel>> expected1 = new ResponseEntity<>(fakeList1, HttpStatus.OK);

personApiModel2.setId(2);
fakeList2.add(personApiModel2);
ResponseEntity<List<PersonApiModel>> expected2 = new ResponseEntity<>(fakeList2, HttpStatus.OK);

PersonApiModel person = personApiModel2;

List<PersonApiModel> expected = new ArrayList<>();
expected.addAll(fakeList1);
expected.addAll(fakeList2);

String cityName = "london";
double distanceInMiles = 20;

Mockito.when(dwpApiService.getAllUsers())
.thenReturn(expected1);

Mockito.when(dwpApiService.getAllUsersInCity(cityName))
.thenReturn(expected2);

Mockito.when(geoService.isLocationWithinDistance(distanceInMiles, cityLatt, cityLong, person))
.thenReturn(true);

List<PersonApiModel> actual = dwpApiService.getAllUsersInCityOrWithinDistanceOfCity(cityName, distanceInMiles);

Assert.assertEquals(expected, actual);
}

现在,使用此测试用例和其他测试用例模拟 DwiApiService 效果很好。但是 mock GeoService 似乎会引起问题。当我尝试运行测试时,它实际上并没有调用模拟方法,而是调用了真实方法。错误不是很有帮助...

[MockitoHint] DwpApiServiceTest.givenMocks_whenGetAllUsersInAndAroundCity_returnMockedPersonList (see javadoc for MockitoHint):
[MockitoHint] 1. Unused... -> at com.dwpAPI.services.DwpApiServiceTest.givenMocks_whenGetAllUsersInAndAroundCity_returnMockedPersonList(DwpApiServiceTest.java:115)
[MockitoHint] ...args ok? -> at com.dwpAPI.services.DwpApiService.getAllUsersInCityOrWithinDistanceOfCity(DwpApiService.java:47)

有人知道这里发生了什么吗?花了几个小时在这上面,但似乎无法弄清楚。

最佳答案

可能的问题是您指定了准确的参数。将 mockito 配置视为一组规则,因此确切的值在那里不起作用(除非你真的很幸运并且它只是神奇地适用于某些特定的狭窄情况)。

所以尝试替换这个:

when(geoService.isLocationWithinDistance(distanceInMiles, cityLatt, cityLong, person))
.thenReturn(true);

有了这个:

when(geoService.isLocationWithinDistance(eq(distanceInMiles), eq(cityLatt), eq(cityLong), eq(person)))
.thenReturn(true);

此外,我有时在使用 when(...).thenReturn(...) 构造时遇到问题 - 例如,它无法与 @Spy 一起正常工作。所以我通常更喜欢这种方法:

doReturn(true).when(geoService)
.isLocationWithinDistance(eq(distanceInMiles), eq(cityLatt), eq(cityLong), eq(person));

如果某些参数对您(或所有参数)不是很重要,请不要使用 eq(...) 并且不要使用任何值 - 只需替换any() 的相关参数。

P.S.:我只更改了一个模拟规则,假设您将自己审查其他规则。

关于java - Mockito 问题 : mock is calling actual method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62375848/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com