Skip to content

Commit a6037c2

Browse files
authored
Fix MockitoMockMaker throws NPE on null object (#2338)
MockMakerRegistry.asMockOrNull() shall return null on null object and not call the mock makers. Fixes #2337
1 parent 5f8de21 commit a6037c2

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

docs/release_notes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include::include.adoc[]
1414

1515
* Fix argument mismatch descriptions for varargs methods by expanding varargs instead of reporting `<too few arguments>` spockPull:2315[]
1616
* Fix Pattern flags being dropped when `java.util.regex.Pattern` instances are used in Spock regex conditions spockIssue:2298[]
17+
* Fix `MockitoMockMaker` throws NPE on null object spockIssue:2337[]
1718

1819
== 2.4 (2025-12-11)
1920

spock-core/src/main/java/org/spockframework/mock/runtime/IMockMaker.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ default IStaticMock makeStaticMock(IMockCreationSettings settings) throws Cannot
125125
* @param object a potential mock object
126126
* @return information about the mock object or {@code null}
127127
*/
128+
@Nullable
128129
default IMockObject asMockOrNull(Object object) {
129130
return null;
130131
}

spock-core/src/main/java/org/spockframework/mock/runtime/MockMakerRegistry.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.spockframework.mock.ISpockMockObject;
2323
import org.spockframework.mock.runtime.IMockMaker.IStaticMock;
2424
import org.spockframework.mock.runtime.IMockMaker.MockMakerCapability;
25-
import org.spockframework.runtime.GroovyRuntimeUtil;
2625
import org.spockframework.util.InternalSpockError;
2726
import org.spockframework.util.Nullable;
2827
import org.spockframework.util.ThreadSafe;
@@ -182,7 +181,11 @@ private static void checkForStaticMockUsageWithClosure(IMockCreationSettings set
182181
* @param object a mock object
183182
* @return information about the mock object
184183
*/
185-
public IMockObject asMockOrNull(Object object) {
184+
@Nullable
185+
public IMockObject asMockOrNull(@Nullable Object object) {
186+
if (object == null) {
187+
return null;
188+
}
186189
if (object instanceof ISpockMockObject) {
187190
return ((ISpockMockObject) object).$spock_get();
188191
}

spock-core/src/main/java/org/spockframework/mock/runtime/mockito/MockitoMockMaker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.spockframework.mock.CannotCreateMockException;
2020
import org.spockframework.mock.IMockObject;
2121
import org.spockframework.mock.runtime.IMockMaker;
22+
import org.spockframework.util.Nullable;
2223
import org.spockframework.util.ReflectionUtil;
2324
import org.spockframework.util.ThreadSafe;
2425
import spock.mock.MockMakerId;
@@ -69,8 +70,9 @@ public int getPriority() {
6970
}
7071

7172
@Override
72-
public IMockObject asMockOrNull(Object object) {
73-
if (impl == null) {
73+
@Nullable
74+
public IMockObject asMockOrNull(@Nullable Object object) {
75+
if (impl == null || object == null) {
7476
return null;
7577
}
7678
return impl.asMockOrNull(object);

spock-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoMockMakerSpec.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ class MockitoMockMakerSpec extends Specification {
4545
mockito.toString() == "$ID default mock maker settings"
4646
}
4747

48+
def "MockitoMockMaker.asMockOrNull() on null returns null"() {
49+
given:
50+
def maker = new MockitoMockMaker()
51+
52+
expect:
53+
maker.asMockOrNull(null) == null
54+
}
55+
4856
def "Verify ID and IMockMakerSettings with Mockito settings"() {
4957
when:
5058
def set = mockito {}
@@ -507,6 +515,19 @@ Can not mock final classes with the following settings :
507515
additionalInterfaceClass.isInstance(m)
508516
mockUtil.isMock(m)
509517
}
518+
519+
@Issue("https://github.com/spockframework/spock/issues/2337")
520+
def "NPE when stubbing method on null object"() {
521+
given:
522+
Object nullObj = null
523+
524+
when:
525+
//Issue #2337: MockitoMockMaker passes null to Mockito's getHandler() throws NPE
526+
nullObj.toString() >> ""
527+
528+
then:
529+
noExceptionThrown()
530+
}
510531
}
511532

512533
class AccessProtectedBaseClass {

0 commit comments

Comments
 (0)