Skip to content

Extracted duplicated stubbings in HttpContentServiceImplTest into 2 helpers - #291

Open
mia-li-001 wants to merge 1 commit into
uPortal-Project:masterfrom
mia-li-001:refactor-duplicated-stubbings-1
Open

mia-li-001 wants to merge 1 commit into
uPortal-Project:masterfrom
mia-li-001:refactor-duplicated-stubbings-1

Conversation

@mia-li-001

Copy link
Copy Markdown

I am a researcher at the university. In my analysis of the project, I observed that

In the test class HttpContentServiceImplTest, 2 groups of stubbings are repeated across tests:

  1. The test testNonForm defines the same stubbings:
    • when(request.getParameter(HttpContentServiceImpl.IS_FORM_PARAM)).thenReturn(getParameterReturnValue)
    • when(processor.process(any(String.class), any(PortletRequest.class))).thenReturn("http:
  2. The tests testPostFormNoParams, testGetForm and testGetFormNoParams define the same stubbings:
    • when(request.getParameter(HttpContentServiceImpl.IS_FORM_PARAM)).thenReturn(getParameterReturnValue)
    • when(processor.process(any(String.class), any(PortletRequest.class))).thenReturn("http:
    • when(request.getParameter(HttpContentServiceImpl.FORM_METHOD_PARAM)).thenReturn(getParameterReturnValue)

Here getParameterReturnValue stands for the value that differs from test to test.

These duplicated stubbings repeat the setup in each test, so a change to that setup has to be made in 1 and 3 places respectively. I propose extracting them into 2 private helper methods:

  • setupSpringELProcessorProcessAndGetParameterStubs, which takes 1 parameter (getParameterReturnValue) and is called from 1 test and is also called by setupSpringELProcessorProcessAndGetParameterStubs2.
  • setupSpringELProcessorProcessAndGetParameterStubs2, which takes 1 parameter (getParameterReturnValue) and adds its own stubbings to those of setupSpringELProcessorProcessAndGetParameterStubs and is called from 3 tests.

This refactoring also reduces the cognitive complexity of HttpContentServiceImplTest by 24.4% (from 104.9 to 79.3, 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant