Skip to content

Extracted duplicated stubbings in ValidatingWebhookTest into a helper - #222

Merged
im-konge merged 1 commit into
strimzi:mainfrom
mia-li-001:refactor-duplicated-stubbings-1
Nov 20, 2026
Merged

im-konge merged 1 commit into
strimzi:mainfrom
mia-li-001:refactor-duplicated-stubbings-1

Conversation

@mia-li-001

Copy link
Copy Markdown
Contributor

Type of Change

Select the type of your PR and delete the other items

  • Bugfix
  • Enhancement / new feature
  • Refactoring
  • Documentation
  • Dependency update
  • Task

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 podCaptor and getReturnValue stand 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 (podCaptor and getReturnValue) and is called from 23 tests.

This refactoring also reduces the cognitive complexity of ValidatingWebhookTest by 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

  • Update documentation
  • Update CHANGELOG.md (if present)
  • Reference relevant issue(s) and close them after merging
  • Write tests
  • Make sure all tests pass
  • Try your changes inside a Kubernetes cluster, not just from unit tests
  • AI assistance was used to create this PR (see the Strimzi AI policy)

@scholzj scholzj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure the getReturnValue makes sense. Maybe you can name it something like currentPod would make more sense.

@scholzj

scholzj commented Nov 2, 2026

Copy link
Copy Markdown
Member

This refactoring also reduces the cognitive complexity of ValidatingWebhookTest by 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.

@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.

@im-konge

Copy link
Copy Markdown
Member

@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>
@mia-li-001
mia-li-001 force-pushed the refactor-duplicated-stubbings-1 branch from 2c867b6 to 1871522 Compare November 19, 2026 19:09
@mia-li-001

Copy link
Copy Markdown
Contributor Author

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 ArgumentCaptor<Pod> is not a stubbing, so it was out of the tool's scope, and that is why the first version of the helper left it out. You are right that it is shared by all these tests, so I moved it into the helper.

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!

@scholzj scholzj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@scholzj
scholzj requested a review from im-konge November 19, 2026 21:06
@scholzj scholzj added this to the 1.9.0 milestone Nov 19, 2026

@im-konge im-konge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@im-konge
im-konge merged commit 069f8d8 into strimzi:main Nov 20, 2026
15 checks passed
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.

3 participants