-
Notifications
You must be signed in to change notification settings - Fork 480
Improve TooManyInvocationsError reporting #2315
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
Merged
leonard84
merged 9 commits into
spockframework:master
from
leonard84:report-unmatched-on-too-many-invocations
Mar 9, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
faba9f0
Improve TooManyInvocationsError reporting
leonard84 5ed9515
Extract shared code
leonard84 5a70029
Remember matched scope
leonard84 582042c
Remove extraneous trim calls
leonard84 65aae0d
Update release notes
leonard84 0ff48ff
Use exhausted instead of unsatisfied...
leonard84 be4d913
Fix nitpick
leonard84 0c764ab
Fix nitpick
leonard84 e73fc94
Merge branch 'master' into report-unmatched-on-too-many-invocations
leonard84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
spock-core/src/main/java/org/spockframework/mock/InteractionDiagnostics.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.spockframework.mock; | ||
|
|
||
| import org.spockframework.util.Assert; | ||
| import org.spockframework.util.IMultiset; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| import static java.util.Collections.sort; | ||
|
|
||
| /** | ||
| * Shared utilities for rendering interaction mismatch diagnostics | ||
| * in {@link TooFewInvocationsError} and {@link TooManyInvocationsError}. | ||
| */ | ||
| class InteractionDiagnostics { | ||
| private static final int MAX_MISMATCH_DESCRIPTIONS = 5; | ||
|
|
||
| /** | ||
| * Score invocations from a multiset against an interaction, preserving counts. | ||
| */ | ||
| static List<ScoredInvocation> scoreInvocations(IMockInteraction interaction, IMultiset<IMockInvocation> invocations) { | ||
| Assert.notNull(interaction); | ||
| List<ScoredInvocation> result = new ArrayList<>(); | ||
| for (Map.Entry<IMockInvocation, Integer> entry : invocations.entrySet()) { | ||
| result.add(new ScoredInvocation(entry.getKey(), entry.getValue(), interaction.computeSimilarityScore(entry.getKey()))); | ||
| } | ||
| sort(result); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Score invocations from a set against an interaction, filtering to only those matching target and method. | ||
| */ | ||
| static List<ScoredInvocation> scoreMatchingInvocations(IMockInteraction interaction, Set<IMockInvocation> invocations) { | ||
| Assert.notNull(interaction); | ||
| List<ScoredInvocation> result = new ArrayList<>(); | ||
| for (IMockInvocation invocation : invocations) { | ||
| if (interaction.matchesTargetAndMethod(invocation)) { | ||
| result.add(new ScoredInvocation(invocation, 0, interaction.computeSimilarityScore(invocation))); | ||
| } | ||
| } | ||
| sort(result); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Append scored invocations with count prefix and mismatch descriptions. | ||
| * Format: {@code count * invocation\ndescribeMismatch\n} | ||
| */ | ||
| static void appendScoredInvocations(StringBuilder builder, IMockInteraction interaction, List<ScoredInvocation> scored) { | ||
| int idx = 0; | ||
| for (ScoredInvocation si : scored) { | ||
| builder.append(si.count); | ||
| builder.append(" * "); | ||
| builder.append(si.invocation); | ||
| builder.append('\n'); | ||
| if (idx++ < MAX_MISMATCH_DESCRIPTIONS) { | ||
| appendMismatchDescription(builder, interaction, si.invocation); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Append only mismatch descriptions for scored invocations (no count/invocation header). | ||
| */ | ||
| static void appendMismatchDescriptions(StringBuilder builder, IMockInteraction interaction, List<ScoredInvocation> scored) { | ||
| int idx = 0; | ||
| for (ScoredInvocation si : scored) { | ||
| if (idx++ < MAX_MISMATCH_DESCRIPTIONS) { | ||
| appendMismatchDescription(builder, interaction, si.invocation); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void appendMismatchDescription(StringBuilder builder, IMockInteraction interaction, IMockInvocation invocation) { | ||
| try { | ||
| builder.append(interaction.describeMismatch(invocation)); | ||
| } catch (AssertionError | Exception e) { | ||
| builder.append("<Renderer threw Exception>: ").append(e.getMessage()); | ||
leonard84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| builder.append('\n'); | ||
| } | ||
|
|
||
| static class ScoredInvocation implements Comparable<ScoredInvocation> { | ||
| final IMockInvocation invocation; | ||
| final int count; | ||
| final int score; | ||
|
|
||
| ScoredInvocation(IMockInvocation invocation, int count, int score) { | ||
| Assert.notNull(invocation); | ||
| this.invocation = invocation; | ||
| this.count = count; | ||
| this.score = score; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(ScoredInvocation other) { | ||
| int result = Integer.compare(score, other.score); | ||
| if (result != 0) return result; | ||
| return invocation.toString().compareTo(other.invocation.toString()); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.