Masalah Mockito: tiruan memanggil metode sebenarnya

Saya mengalami masalah dengan Mockito di mana metode sebenarnya dipanggil dalam pengujian daripada metode tiruan. Mencari berjam-jam namun belum juga menemukan jawaban yang cocok.

Inilah layanan yang saya uji:

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

}

Layanan ini memanggil kelas GeoService berikut:

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;
    }
}

Kasus uji saya terlihat seperti ini:

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

Sekarang, mengejek DwiApiService berfungsi dengan baik, dengan test case ini dan lainnya. Namun mengejek GeoService tampaknya menimbulkan masalah. Ketika saya mencoba menjalankan tes, itu tidak benar-benar memanggil metode yang diolok-olok tetapi yang asli. Kesalahannya tidak terlalu membantu...

[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)

Adakah yang tahu apa yang terjadi di sini? Menghabiskan waktu berjam-jam untuk hal ini dan sepertinya tidak dapat menemukan jawabannya.


person soFreshSoClean    schedule 14.06.2020    source sumber
comment
Coba lakukan pengkabelan otomatis geoService alih-alih menginisialisasi dengan new.   -  person Smile    schedule 14.06.2020
comment
Mencobanya, mendapatkan kesalahan yang sama.   -  person soFreshSoClean    schedule 14.06.2020
comment
Apakah ini menjawab pertanyaan Anda? Mockito - perbedaan antara doReturn() dan kapan()   -  person Progman    schedule 14.06.2020
comment
@Progman Mencobanya tetapi sepertinya tidak berhasil. Stepio di bawah menemukan jawaban yang berhasil. Namun tidak sepenuhnya yakin mengapa!   -  person soFreshSoClean    schedule 14.06.2020


Jawaban (2)


Kemungkinan masalahnya adalah Anda menentukan parameter yang tepat. Pertimbangkan konfigurasi mockito sebagai seperangkat aturan, jadi nilai pastinya tidak berfungsi di sana (kecuali Anda benar-benar beruntung dan secara ajaib berfungsi untuk beberapa kasus sempit tertentu).

Jadi coba ganti yang ini:

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

Dengan ini:

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

Saya juga terkadang mengalami masalah dalam menggunakan konstruksi when(...).thenReturn(...) - tidak berfungsi dengan baik dengan @Spy, misalnya. Jadi saya biasanya lebih memilih pendekatan ini:

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

Dan jika beberapa parameter tidak terlalu penting bagi Anda (atau semuanya), jangan gunakan eq(...) dan jangan gunakan nilai apa pun - ganti saja parameter yang relevan dengan any().

P.S.: Saya hanya mengubah satu aturan yang diolok-olok, dengan asumsi Anda akan meninjau aturan lain sendiri.

person stepio    schedule 14.06.2020
comment
Hmmm saya ganti when(geoService.isLocationWithinDistance(distanceInMiles, cityLatt, cityLong, person)).thenReturn(true); dengan when(geoService.isLocationWithinDistance(anyDouble(), anyDouble(), anyDouble(), any())).thenReturn(true); dan berhasil! Tidak yakin mengapa, jadi saya akan menggalinya lebih dalam. Terima kasih! - person soFreshSoClean; 14.06.2020

alasannya mungkin karena Anda langsung menggunakannya

private GeoService geoService = new GeoService();

Anda mungkin perlu memberi anotasi pada kelas GeoService dengan @Service dan mengotomatiskannya di DwpApiService

person Hamza    schedule 14.06.2020
comment
Menambahkan anotasi @Service ke kelas GeoService dan @Autowired ke referensi GeoService di kelas DwiApiService. Masih mendapatkan kesalahan yang sama. - person soFreshSoClean; 14.06.2020