Skip to content

Java: port quality query java/mocking-all-non-private-methods-means-unit-test-is-too-big #20205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Napalys
Copy link
Contributor

@Napalys Napalys commented Aug 11, 2025

Ported the java/mocking-all-non-private-methods-means-unit-test-is-too-big query. Key changes:

  • Classes with only 1 public method being mocked are no longer flagged as true positives.
  • MRVA 1000 reduced from 1,332 to 250 results.
  • DCA https://github.com/github/codeql-dca-main/issues/30679
  • Autofix: Primarily focuses on trimming test cases to test a single method, which may or may not be considered as a acceptable fix as it may now not cover trimmed methods.

@Napalys Napalys added the no-change-note-required This PR does not need a change note label Aug 11, 2025
@Napalys Napalys force-pushed the java/mocking-all-non-private-methods-means-unit-test-is-too-big branch from f4a5e1e to cdb0d25 Compare August 11, 2025 11:43
@Napalys Napalys force-pushed the java/mocking-all-non-private-methods-means-unit-test-is-too-big branch from cdb0d25 to ff648fc Compare August 11, 2025 11:43
@Napalys Napalys marked this pull request as ready for review August 11, 2025 12:31
@Napalys Napalys requested a review from a team as a code owner August 11, 2025 12:31
@Copilot Copilot AI review requested due to automatic review settings August 11, 2025 12:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Ports the java/mocking-all-non-private-methods-means-unit-test-is-too-big query to detect unit tests that mock all public methods of a class, which may indicate the test is testing too much functionality.

Key changes:

  • Added logic to exclude classes with only 1 public method from being flagged as violations
  • Reduced false positive results from 1,332 to 250 on MRVA 1000
  • Added comprehensive test stubs for Mockito 5.14 and JUnit 4.13 to support the query functionality

Reviewed Changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
java/ql/src/Likely Bugs/Frameworks/JUnit/MockingAllNonPrivateMethodsMeansUnitTestIsTooBig.ql Main query implementation that detects excessive mocking patterns
java/ql/src/Likely Bugs/Frameworks/JUnit/MockingAllNonPrivateMethodsMeansUnitTestIsTooBig.md Documentation explaining the rule and providing usage examples
java/ql/test/query-tests/MockingAllNonPrivateMethodsMeansUnitTestIsTooBig/ Test cases with expected results demonstrating compliant and non-compliant patterns
java/ql/test/stubs/mockito-5.14/ Mockito framework stubs for testing support
java/ql/test/stubs/junit-4.13/ JUnit 4.13 framework stubs for testing support
java/ql/integration-tests/java/query-suite/*.qls.expected Updated query suite expectations to include the new rule

mockCall.getParent+() = testMethod.getBody().getAStmt() and
mockedClassOrInterface = mockCall.getMockedType() and
// Only flag classes with multiple public methods (2 or more)
count(Method m | m = mockedClassOrInterface.getAMethod() and m.isPublic()) > 1 and
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting this count condition into a predicate for better readability and reusability. For example, create a predicate hasMultiplePublicMethods(ClassOrInterface c) that encapsulates this logic.

Suggested change
count(Method m | m = mockedClassOrInterface.getAMethod() and m.isPublic()) > 1 and
hasMultiplePublicMethods(mockedClassOrInterface) and

Copilot uses AI. Check for mistakes.

@Napalys Napalys requested a review from knewbury01 August 11, 2025 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Java no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant