使用 Mockito 模拟带有对象参数的方法
Mock a method with an object parameter with Mockito
在我的单元测试中,我想通过执行以下操作来模拟与 elasticsearch 的交互
when(cityDefinitionRepository.findCitiesNearby(geoPoint, SOURCE, 2)).thenReturn(cityDefinitionsArrival);
when(cityDefinitionRepository.findCitiesNearby(geoPoint2, SOURCE, 2)).thenReturn(cityDefinitionsDeparture);
SearchResult results = benerailService.doSearch(interpretation, 2, false);
doSearch 方法包含
departureCityDefinitions = cityDefinitionRepository.findCitiesNearby(geo, SOURCE, distance);
当我调试我的代码时,我看到在我的 doSearch 方法中调用了 mockito,但它没有 return cityDefinitionsArrival 对象。这可能是因为 geoPoint 和 geo 是两个不同的对象。
geoPoint和geo对象都是包含相同经纬度的elasticsearch GeoPoints。
我通过
设法让它工作
when(cityDefinitionRepository.findCitiesNearby(any(geoPoint.getClass()), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsArrival);
when(cityDefinitionRepository.findCitiesNearby(any(geoPoint2.getClass()), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsDeparture);
但现在它忽略我的纬度和经度值并接受 GeoPoint class 的任何对象。这是一个问题,因为在我的 doSearch 方法中,我有两个 findCitiesNearby 用法,每个都有不同的纬度和经度,我需要分别模拟它们。
Mockito 可以吗?
cityDefinitionsArrival和cityDefinitionsDeparture都是ArrayLists,
SOURCE 是一个字符串值,并且
geo 和 geoPoint 对象:
GeoPoint geoPoint = new GeoPoint(50.850449999999995, 4.34878);
GeoPoint geoPoint2 = new GeoPoint(48.861710, 2.348923);
double lat = 50.850449999999995;
double lon = 4.34878;
GeoPoint geo = new GeoPoint(lat, lon);
double lat2 = 48.861710;
double lon2 = 2.348923;
GeoPoint geo2 = new GeoPoint(lat2, lon2);
public final class IsSameLatLong extends ArgumentMatcher<GeoPoint> {
private final GeoPoint as;
public IsSameLatLong(GeoPoint as) {
this.as = as;
}
//some sensible value, like 1000th of a second i.e. 0° 0' 0.001"
private final static double EPSILON = 1.0/(60*60*1000);
private static boolean closeEnough(double a, double b) {
return Math.abs(a - b) < EPSILON;
}
public boolean matches(Object point) {
GeoPoint other = (GeoPoint) point;
if (other == null) return false;
return closeEnough(other.getLat(), as.getLat()) &&
closeEnough(other.getLong(), as.getLong());
}
}
然后像这样使用:
when(cityDefinitionRepository.findCitiesNearby(argThat(new IsSameLatLong(geoPoint)), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsArrival);
when(cityDefinitionRepository.findCitiesNearby(argThat(new IsSameLatLong(geoPoint2)), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsDeparture);
在我的单元测试中,我想通过执行以下操作来模拟与 elasticsearch 的交互
when(cityDefinitionRepository.findCitiesNearby(geoPoint, SOURCE, 2)).thenReturn(cityDefinitionsArrival);
when(cityDefinitionRepository.findCitiesNearby(geoPoint2, SOURCE, 2)).thenReturn(cityDefinitionsDeparture);
SearchResult results = benerailService.doSearch(interpretation, 2, false);
doSearch 方法包含
departureCityDefinitions = cityDefinitionRepository.findCitiesNearby(geo, SOURCE, distance);
当我调试我的代码时,我看到在我的 doSearch 方法中调用了 mockito,但它没有 return cityDefinitionsArrival 对象。这可能是因为 geoPoint 和 geo 是两个不同的对象。
geoPoint和geo对象都是包含相同经纬度的elasticsearch GeoPoints。
我通过
设法让它工作when(cityDefinitionRepository.findCitiesNearby(any(geoPoint.getClass()), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsArrival);
when(cityDefinitionRepository.findCitiesNearby(any(geoPoint2.getClass()), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsDeparture);
但现在它忽略我的纬度和经度值并接受 GeoPoint class 的任何对象。这是一个问题,因为在我的 doSearch 方法中,我有两个 findCitiesNearby 用法,每个都有不同的纬度和经度,我需要分别模拟它们。
Mockito 可以吗?
cityDefinitionsArrival和cityDefinitionsDeparture都是ArrayLists, SOURCE 是一个字符串值,并且 geo 和 geoPoint 对象:
GeoPoint geoPoint = new GeoPoint(50.850449999999995, 4.34878);
GeoPoint geoPoint2 = new GeoPoint(48.861710, 2.348923);
double lat = 50.850449999999995;
double lon = 4.34878;
GeoPoint geo = new GeoPoint(lat, lon);
double lat2 = 48.861710;
double lon2 = 2.348923;
GeoPoint geo2 = new GeoPoint(lat2, lon2);
public final class IsSameLatLong extends ArgumentMatcher<GeoPoint> {
private final GeoPoint as;
public IsSameLatLong(GeoPoint as) {
this.as = as;
}
//some sensible value, like 1000th of a second i.e. 0° 0' 0.001"
private final static double EPSILON = 1.0/(60*60*1000);
private static boolean closeEnough(double a, double b) {
return Math.abs(a - b) < EPSILON;
}
public boolean matches(Object point) {
GeoPoint other = (GeoPoint) point;
if (other == null) return false;
return closeEnough(other.getLat(), as.getLat()) &&
closeEnough(other.getLong(), as.getLong());
}
}
然后像这样使用:
when(cityDefinitionRepository.findCitiesNearby(argThat(new IsSameLatLong(geoPoint)), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsArrival);
when(cityDefinitionRepository.findCitiesNearby(argThat(new IsSameLatLong(geoPoint2)), eq(SOURCE), eq(2))).thenReturn(cityDefinitionsDeparture);