vendredi 4 décembre 2020

Mockito - how to access .when argument in .thenReturn method?

When the .get() method on my getter object is called, it usually sends a request to an API. For the mock version of the Getter object, I want .getFile() to return the result of calling reader.readLocalFile() with the same argument (to avoid making a real HTTP request).

My current code is in the form:

Mockito.when(mockGetter.get(file1)).thenReturn(reader.readLocalFile(file1));
Mockito.when(mockGetter.get(file2)).thenReturn(reader.readLocalFile(file2));
Mockito.when(mockGetter.get(file3)).thenReturn(reader.readLocalFile(file3));
(etc.)

where file1, file2 and file3 are string literals.

Would it be possible to make it look more like the following?

Mockito.when(mockGetter.get(someString)).thenReturn(reader.readLocalFile(thatSameString));

Aucun commentaire:

Enregistrer un commentaire