Skip to content

Commit

Permalink
microsoft tests: delete pointless mocks, make test more maintainable (#…
Browse files Browse the repository at this point in the history
…1405)

What's wrong: we already were constructing real objects (fakes) as test-doubles, just pass the entire thing through rather than piecemealing the entire api surface with a ton of mock() calls.

why fix this: we already spent human-time on this since I had to debug why the tests were failing in a previous PR. This commit just saves the next person re-debugging this.

nit: unclear where 5 came from, but I can reason about 3, so use that (but a bit more that self-documenting for the future)
  • Loading branch information
jzacsh authored Dec 6, 2024
1 parent c3af74e commit 9b45db1
Showing 1 changed file with 16 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.argThat;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -145,16 +146,10 @@ public void testCleanAlbumNames() throws Exception {
&& body.contains("album1_");
}));
Response response = mock(Response.class);
ResponseBody body = mock(ResponseBody.class);
when(body.bytes())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").bytes());
when(body.string())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").string());
when(response.code()).thenReturn(200);
when(response.message()).thenReturn("OK");
when(response.body()).thenReturn(body);
when(response.body())
.thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}"));
when(call.execute()).thenReturn(response);

ImportResult result = importer.importItem(uuid, executor, authData, data);
Expand All @@ -180,16 +175,10 @@ public void testImportItemPermissionDenied() throws Exception {
.toString()
.equals("https://www.baseurl.com/v1.0/me/drive/special/photos/children")));
Response response = mock(Response.class);
ResponseBody body = mock(ResponseBody.class);
when(body.bytes())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").bytes());
when(body.string())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").string());
when(response.code()).thenReturn(403);
when(response.message()).thenReturn("Access Denied");
when(response.body()).thenReturn(body);
when(response.body())
.thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}"));
when(call.execute()).thenReturn(response);

assertThrows(
Expand Down Expand Up @@ -289,62 +278,40 @@ public void testImportItemAllSuccess() throws Exception {
.toString()
.equals("https://www.baseurl.com/v1.0/me/drive/special/photos/children")));
Response response = mock(Response.class);
ResponseBody body = mock(ResponseBody.class);
when(body.bytes())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").bytes());
when(body.string())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").string());
when(response.code()).thenReturn(200);
when(response.message()).thenReturn("OK");
when(response.body()).thenReturn(body);
when(response.body())
.thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}"));
when(call.execute()).thenReturn(response);

Call call2 = mock(Call.class);
doReturn(call2)
.when(client)
.newCall(argThat((Request r) -> r.url().toString().contains("createUploadSession")));
Response response2 = mock(Response.class);
ResponseBody body2 = mock(ResponseBody.class);
when(body2.bytes())
.thenReturn(
ResponseBody.create(
MediaType.parse("application/json"),
"{\"uploadUrl\": \"https://scalia.com/link\"}")
.bytes());
when(body2.string())
.thenReturn(
ResponseBody.create(
MediaType.parse("application/json"),
"{\"uploadUrl\": \"https://scalia.com/link\"}")
.string());
when(response2.code()).thenReturn(200);
when(response2.message()).thenReturn("OK");
when(response2.body()).thenReturn(body2);
when(response2.body())
.thenReturn(
ResponseBody.create(
MediaType.parse("application/json"),
"{\"uploadUrl\": \"https://scalia.com/link\"}"));
when(call2.execute()).thenReturn(response2);

Call call3 = mock(Call.class);
doReturn(call3)
.when(client)
.newCall(argThat((Request r) -> r.url().toString().contains("scalia.com/link")));
Response response3 = mock(Response.class);
ResponseBody body3 = mock(ResponseBody.class);
when(body3.bytes())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}")
.bytes());
when(body3.string())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}")
.string());
when(response3.code()).thenReturn(200);
when(response3.message()).thenReturn("OK");
when(response3.body()).thenReturn(body3);
when(response3.body())
.thenReturn(
ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}"));
when(call3.execute()).thenReturn(response3);

ImportResult result = importer.importItem(uuid, executor, authData, data);
verify(client, times(5)).newCall(any());
verify(client, atLeast(albums.size() + photos.size())).newCall(any());
assertThat(result).isEqualTo(ImportResult.OK);
}
}

0 comments on commit 9b45db1

Please sign in to comment.