Repository navigation
Extracted duplicated stubbings in ValidatingWebhookTest into a helper - #222
Conversation
scholzj
left a comment
There was a problem hiding this comment.
Thanks for the PR. Please:
- Fix the DCO signoff. The instructions should be under the Details link next to the DCO status failure.
- If you used AI to create the PR, make sure to properly disclose it in the PR description
- Why does the helper method not include the PodCaptor? It seems to be shared by all the tests as well. So you can probably create it in the method and have it returned?
| assertThat(podCaptor.getValue().getMetadata().getAnnotations().get("strimzi.io/manual-rolling-update"), is("true")); | ||
| } | ||
|
|
||
| private void setupEditReplacePatchablePatchAndGetStubs(ArgumentCaptor<Pod> podCaptor, Pod getReturnValue) { |
There was a problem hiding this comment.
Not sure the getReturnValue makes sense. Maybe you can name it something like currentPod would make more sense.
@mia-li-001 For the record, I think this PR make the tests easier to maintain. But it does not necessarily help to understand the code by replacing two lines that are easy to read by call to a method. I think overall it is a good PR that improves the code. But at least I personally do not think it helps the way the metrics suggest. |
|
@mia-li-001 are you still interested in this PR? Jakub had good points to the PR, so if you are still interested, could you please have a look at them, so we can proceed? Thanks |
Signed-off-by: mia-li-001 <mialmz810@gmail.com>
2c867b6 to
1871522
Compare
|
Thanks for the review, @scholzj, and sorry for the slow reply, @im-konge. For context: this PR came from a research tool we built that analyzes duplicated stubbings (stubbing clones) in test code. The We understand the trade-off between easier maintenance and the readability of each test, so we are happy to leave it to you to decide whether this change is worth merging. Thank you again for your time and feedback! |
Type of Change
Select the type of your PR and delete the other items
Description
Please describe your pull request
I am a researcher at the university. In my analysis of the project, I observed that
In the test class
ValidatingWebhookTest, 23 tests define the same stubbings:when(podResource.get()).thenReturn(getReturnValue)when(podResource.patch(podCaptor.capture())).thenReturn(new Pod())Here
podCaptorandgetReturnValuestand 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 23 places. I propose extracting them into 1 private helper method:
setupEditReplacePatchablePatchAndGetStubs, which takes 2 parameters (podCaptorandgetReturnValue) and is called from 23 tests.This refactoring also reduces the cognitive complexity of
ValidatingWebhookTestby 10.9% (from 1369.6 to 1220.9, 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 helper. 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.
Checklist
Please go through this checklist and make sure all applicable tasks have been done