Skip to content

Commit a853aae

Browse files
authored
Merge pull request #1387 from gzhao9/refactor-OkHttpRequestAdapterTest
Refactor to Eliminate Repetitive Mock Object Creation in OkHttpRequestAdapterTest.java
2 parents 7dd136d + 26e72a1 commit a853aae

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ void sendReturnsNullOnNoContent(int statusCode) throws Exception {
167167
httpMethod = HttpMethod.GET;
168168
}
169169
};
170-
final var mockEntity = mock(Parsable.class);
171-
when(mockEntity.getFieldDeserializers()).thenReturn(new HashMap<>());
170+
final var mockEntity = creatMockEntity();
172171
final var response = requestAdapter.send(requestInformation, null, (node) -> mockEntity);
173172
assertNull(response);
174173
}
@@ -198,14 +197,9 @@ void sendReturnsObjectOnContent(int statusCode) throws Exception {
198197
httpMethod = HttpMethod.GET;
199198
}
200199
};
201-
final var mockEntity = mock(Parsable.class);
202-
when(mockEntity.getFieldDeserializers()).thenReturn(new HashMap<>());
203-
final var mockParseNode = mock(ParseNode.class);
204-
when(mockParseNode.getObjectValue(any(ParsableFactory.class))).thenReturn(mockEntity);
205-
final var mockFactory = mock(ParseNodeFactory.class);
206-
when(mockFactory.getParseNode(any(String.class), any(InputStream.class)))
207-
.thenReturn(mockParseNode);
208-
when(mockFactory.getValidContentType()).thenReturn("application/json");
200+
final var mockEntity = creatMockEntity();
201+
final var mockParseNode = creatMockParseNode(mockEntity);
202+
final var mockFactory = creatMockParseNodeFactory(mockParseNode, "application/json");
209203
final var requestAdapter =
210204
new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client);
211205
final var response = requestAdapter.send(requestInformation, null, (node) -> mockEntity);
@@ -260,16 +254,11 @@ void throwsAPIException(
260254
httpMethod = HttpMethod.GET;
261255
}
262256
};
263-
final var mockEntity = mock(Parsable.class);
264-
when(mockEntity.getFieldDeserializers()).thenReturn(new HashMap<>());
257+
final var mockEntity = creatMockEntity();
265258
final var mockParsableFactory = mock(ParsableFactory.class);
266259
when(mockParsableFactory.create(any(ParseNode.class))).thenReturn(mockEntity);
267-
final var mockParseNode = mock(ParseNode.class);
268-
when(mockParseNode.getObjectValue(any(ParsableFactory.class))).thenReturn(mockEntity);
269-
final var mockFactory = mock(ParseNodeFactory.class);
270-
when(mockFactory.getParseNode(any(String.class), any(InputStream.class)))
271-
.thenReturn(mockParseNode);
272-
when(mockFactory.getValidContentType()).thenReturn("application/json");
260+
final var mockParseNode = creatMockParseNode(mockEntity);
261+
final var mockFactory = creatMockParseNodeFactory(mockParseNode, "application/json");
273262

274263
final var requestAdapter =
275264
new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client);
@@ -430,4 +419,25 @@ public static OkHttpClient getMockClient(final Response response) throws IOExcep
430419
when(mockClient.newCall(any())).thenReturn(remoteCall);
431420
return mockClient;
432421
}
422+
423+
public Parsable creatMockEntity() {
424+
final var mockEntity = mock(Parsable.class);
425+
when(mockEntity.getFieldDeserializers()).thenReturn(new HashMap<>());
426+
return mockEntity;
427+
}
428+
429+
public ParseNode creatMockParseNode(Parsable entity) {
430+
final var mockParseNode = mock(ParseNode.class);
431+
when(mockParseNode.getObjectValue(any(ParsableFactory.class))).thenReturn(entity);
432+
return mockParseNode;
433+
}
434+
435+
public ParseNodeFactory creatMockParseNodeFactory(
436+
ParseNode mockParseNode, String validContentType) {
437+
final var mockFactory = mock(ParseNodeFactory.class);
438+
when(mockFactory.getParseNode(any(String.class), any(InputStream.class)))
439+
.thenReturn(mockParseNode);
440+
when(mockFactory.getValidContentType()).thenReturn(validContentType);
441+
return mockFactory;
442+
}
433443
}

0 commit comments

Comments
 (0)