Skip to content

Fix for #2352 - Generic Comparable Stubs/Mocks ==#2353

Open
shysteph wants to merge 1 commit into
spockframework:masterfrom
shysteph:master
Open

Fix for #2352 - Generic Comparable Stubs/Mocks ==#2353
shysteph wants to merge 1 commit into
spockframework:masterfrom
shysteph:master

Conversation

@shysteph
Copy link
Copy Markdown

@shysteph shysteph commented May 15, 2026

They should come out as unequal like different Stubs/Mocks for a non Comparable class would. Extends the work for #1322 to handle Generics. Does this by considering any method named compareTo with a single argument that is a Comprable to be a Comparable.compareTo method.

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced mock argument matching to properly handle generic Comparable implementations alongside standard Comparable types, improving compatibility with parameterized comparable classes.
  • Tests

    • Added test coverage to verify that non-matching instances of generic comparable mock stubs are correctly treated as unequal in mock interactions.

Review Change Stack

They should come out as unequal like different Stubs/Mocks for
 a non Comparable class would. Extends the work for spockframework#1322 to
 handle Generics. Does this by considering any method named
 compareTo with a single argument that is a Comprable to be a
 Comparable.compareTo method.
Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b2620343-bb51-418c-944a-2c562096a542

📥 Commits

Reviewing files that changed from the base of the PR and between 8fc2840 and 527996b.

📒 Files selected for processing (3)
  • spock-core/src/main/java/org/spockframework/mock/DefaultCompareToInteraction.java
  • spock-specs/src/test/groovy/org/spockframework/smoke/mock/ArgumentMatching.groovy
  • spock-specs/src/test/java/org/spockframework/smoke/mock/GenericComparable.java

📝 Walkthrough

Walkthrough

Enhanced mock argument matching to recognize compareTo() for generic Comparable<T> implementations by recursively detecting all interfaces implemented by a method parameter type. Added a test fixture and smoke test verifying that non-matching generic Comparable stubs are unequal.

Changes

Generic Comparable Argument Matching

Layer / File(s) Summary
Comparable interface detection and matching logic
spock-core/src/main/java/org/spockframework/mock/DefaultCompareToInteraction.java
matches() now accepts compareTo when parameter type is Object or implements Comparable via a new recursive interface scan. Added getInterfacesFromClass() helper that collects all interfaces from a class and its superclass hierarchy. Formatting adjusted in accept() lambda.
Generic Comparable test fixture and smoke test
spock-specs/src/test/java/org/spockframework/smoke/mock/GenericComparable.java, spock-specs/src/test/groovy/org/spockframework/smoke/mock/ArgumentMatching.groovy
Introduced GenericComparable class implementing Comparable<GenericComparable> with immutable int value and delegation to Integer.compare(). New smoke test verifies two non-matching GenericComparable stubs are unequal via != operator.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 A rabbit hops through interfaces deep,
Finding Comparable in inheritance's keep,
Generic types now match with care,
When compareTo dances through the air! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix for #2352 - Generic Comparable Stubs/Mocks ==' is directly related to the main change, clearly identifying a bug fix for issue #2352 regarding generic Comparable stubs/mocks, which matches the PR objectives.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Member

@AndreasTu AndreasTu left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!
I have some small changes.

return () -> Integer.compare(System.identityHashCode(invocation.getMockObject().getInstance()), System.identityHashCode(invocation.getArguments().get(0)));
}

private Set<Class<?>> getInterfacesFromClass(Class<?> clazz) {
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.

This helper method should probably be moved into org.spockframework.util.ReflectionUtil and please add one of two tests for the util method to ReflectionUtilSpec

@@ -1,5 +1,7 @@
package org.spockframework.mock;

import java.util.HashSet;
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.

Please add a line to the release notes in docs/release_notes.adoc for version 2.5 section Misc.

@@ -0,0 +1,16 @@
package org.spockframework.smoke.mock;

import org.jetbrains.annotations.NotNull;
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.

We do not use the jetbrains nullable annotations.
We only use the internal org.spockframework.util.Nullable everything else in NonNull per conventions.
So please remove.

@AndreasTu AndreasTu added this to the 2.5 milestone May 23, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 23, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.17%. Comparing base (8fc2840) to head (527996b).

Files with missing lines Patch % Lines
...ockframework/mock/DefaultCompareToInteraction.java 81.81% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2353      +/-   ##
============================================
+ Coverage     82.16%   82.17%   +0.01%     
- Complexity     4825     4832       +7     
============================================
  Files           472      472              
  Lines         15040    15049       +9     
  Branches       1908     1912       +4     
============================================
+ Hits          12357    12366       +9     
  Misses         1991     1991              
  Partials        692      692              
Files with missing lines Coverage Δ
...ockframework/mock/DefaultCompareToInteraction.java 76.47% <81.81%> (+26.47%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants