Repository navigation
Extracted duplicated stubbings in HttpClientDefaultTest into 2 helpers - #259
Open
mia-li-001 wants to merge 1 commit into
Open
mia-li-001 wants to merge 1 commit into
mia-li-001 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I am a researcher at the university. In my analysis of the project, I observed that
In the test class
HttpClientDefaultTest, 2 groups of stubbings are repeated across tests:testGetInputStreamThrowsandtestDisconnectThrowsdefine the same stubbings:when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream())when(connection.getResponseCode()).thenReturn(getResponseCodeReturnValue)testGetInputStreamReturnsNull,testIsOKWhenNotOkandtestIsOKWhenNotOkStatusCodeLessThan200define the same stubbings:when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream())when(connection.getResponseCode()).thenReturn(getResponseCodeReturnValue)when(connection.getInputStream()).thenReturn(null)Here
connectionandgetResponseCodeReturnValuestand for the values that differ from test to test.These duplicated stubbings repeat the setup in each test, so a change to that setup has to be made in 2 and 3 places respectively. I propose extracting them into 2 private helper methods:
setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs, which takes 2 parameters (connectionandgetResponseCodeReturnValue) and is called from 2 tests and is also called bysetupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs.setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs, which takes 2 parameters (connectionandgetResponseCodeReturnValue) and adds its own stubbings to those ofsetupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubsand is called from 3 tests.This refactoring also reduces the cognitive complexity of
HttpClientDefaultTestby 22.5% (from 87.3 to 67.7, measured with Genese). Cognitive complexity, introduced by SonarSource, estimates how hard code is for a human to understand.The change only moves the existing stubbings into the helpers. The stubbed calls, arguments and returned values are the same as before, and the test results are unchanged.
I propose a solution below to remove the duplicated stubbings. Happy to modify the pull request based on your feedback.