Skip to content

fix(mock): throw on interaction with null target - #2402

Open
arimu1 wants to merge 1 commit into
spockframework:masterfrom
arimu1:fix/2339-null-target-interaction
Open

fix(mock): throw on interaction with null target#2402
arimu1 wants to merge 1 commit into
spockframework:masterfrom
arimu1:fix/2339-null-target-interaction

Conversation

@arimu1

@arimu1 arimu1 commented Aug 7, 2026

Copy link
Copy Markdown

Summary

Declaring an interaction on a null target (for example after an unstubbed mock chain returns null) is invalid. Previously Spock accepted such interactions silently, which made the mistake hard to notice. Some mock makers also failed with an unrelated NPE (see #2337).

This change rejects a null interaction target with InvalidSpecException and the message Interaction on a null object is invalid.

AI assistance: Used for issue research and drafting the change; the design follows existing InvalidSpecException validation in TargetConstraint / InteractionBuilder, and the tests were run locally.

Changes

  • TargetConstraint: throw InvalidSpecException when the target is null
  • Smoke tests for stubbing and required interactions on null targets
  • Update the Mockito null-object regression from MockitoMockMaker throws NPE on null object #2337 to expect the new exception (still proves no NPE into Mockito)
  • Release notes entry under Breaking Changes for 2.5

Testing

./gradlew :spock-specs:test \
  --tests 'org.spockframework.smoke.mock.NullTargetInteraction' \
  --tests 'org.spockframework.mock.runtime.mockito.MockitoMockMakerSpec' \
  -DjavaVersion=8

Result: BUILD SUCCESSFUL (Temurin 21 Gradle JVM, Java 8 toolchain for the variant).

Fixes #2339

Declaring an interaction on a null target is invalid and was previously
silent (or could NPE with some mock makers). Reject it early with
InvalidSpecException so users get a clear error.

Fixes spockframework#2339
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: afccfc6b-974a-4069-8a41-91b16970245e

📥 Commits

Reviewing files that changed from the base of the PR and between 37e5e9a and 8bf5d20.

📒 Files selected for processing (4)
  • docs/release_notes.adoc
  • spock-core/src/main/java/org/spockframework/mock/constraint/TargetConstraint.java
  • spock-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoMockMakerSpec.groovy
  • spock-specs/src/test/groovy/org/spockframework/smoke/mock/NullTargetInteraction.groovy

📝 Walkthrough

Walkthrough

Interactions declared on null targets now throw InvalidSpecException with a defined message. Tests cover stubbing, required interactions, valid mocks, and Mockito integration. Release notes document this breaking change.

Changes

Null interaction validation

Layer / File(s) Summary
Target validation
spock-core/src/main/java/org/spockframework/mock/constraint/TargetConstraint.java
TargetConstraint now rejects null targets with InvalidSpecException.
Regression coverage and release note
spock-specs/src/test/groovy/org/spockframework/smoke/mock/NullTargetInteraction.groovy, spock-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoMockMakerSpec.groovy, docs/release_notes.adoc
Tests cover invalid null interactions and valid mock interactions. The release note documents the breaking change.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: andreastu, leonard84

Poem

A bunny taps a null-bound call,
“InvalidSpecException” guards them all.
Mock lists still hop along,
Clear tests tell what went wrong.
Release notes mark the change—
Carrots sorted, behavior arranged.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: throwing an exception for interactions with null targets.
Description check ✅ Passed The description accurately explains the null-target behavior change, affected code, tests, release note, and linked issue.
Linked Issues check ✅ Passed The implementation satisfies issue #2339 by rejecting null-target interactions with InvalidSpecException and adding regression coverage.
Out of Scope Changes check ✅ Passed The code, tests, Mockito regression update, and release note are directly related to the null-target interaction requirement.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

@greptile-apps

greptile-apps Bot commented Aug 7, 2026

Copy link
Copy Markdown

Greptile Summary

This PR rejects mock interactions whose evaluated target is null, replacing silent acceptance or mock-maker-specific failures with a consistent InvalidSpecException.

  • Adds the null-target guard to TargetConstraint.
  • Adds smoke coverage for stubbing, required interactions, and valid mock targets.
  • Updates the Mockito regression test and documents the breaking change in the 2.5 release notes.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified.

The new validation applies uniformly through the shared interaction-target construction path, preserves wildcard interactions through their non-null sentinel, and is covered for both stubbing and required interactions.

Important Files Changed

Filename Overview
spock-core/src/main/java/org/spockframework/mock/constraint/TargetConstraint.java Adds an early, consistent validation error for null interaction targets without affecting wildcard targets.
spock-specs/src/test/groovy/org/spockframework/smoke/mock/NullTargetInteraction.groovy Covers null-target stubbing and required interactions, plus a positive control for ordinary mocks.
spock-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoMockMakerSpec.groovy Updates the Mockito regression to assert the framework-level exception rather than silent acceptance.
docs/release_notes.adoc Clearly records the new null-target rejection as a breaking change.

Reviews (1): Last reviewed commit: "fix(mock): throw on interaction with nul..." | Re-trigger Greptile

@AndreasTu

AndreasTu commented Aug 9, 2026

Copy link
Copy Markdown
Member

Hi @arimu1,
thanks for the contribution, but this PR needs to wait for version 3.0 as stated in the issue, due to the breaking change.

Version 3.0 is not planed yet.
I will mark this PR with the milestone 3.0 and we will come back to it, when 3.0 is in progress.

@AndreasTu AndreasTu added this to the 3.0 milestone Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interaction on null mock object shall throw exception

2 participants