File tree Expand file tree Collapse file tree
spock-core/src/main/java/org/spockframework/mock/runtime
spock-specs/src/test/groovy/org/spockframework/mock/runtime/mockito Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 2222import org .spockframework .mock .ISpockMockObject ;
2323import org .spockframework .mock .runtime .IMockMaker .IStaticMock ;
2424import org .spockframework .mock .runtime .IMockMaker .MockMakerCapability ;
25- import org .spockframework .runtime .GroovyRuntimeUtil ;
2625import org .spockframework .util .InternalSpockError ;
2726import org .spockframework .util .Nullable ;
2827import 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 }
Original file line number Diff line number Diff line change 1919import org .spockframework .mock .CannotCreateMockException ;
2020import org .spockframework .mock .IMockObject ;
2121import org .spockframework .mock .runtime .IMockMaker ;
22+ import org .spockframework .util .Nullable ;
2223import org .spockframework .util .ReflectionUtil ;
2324import org .spockframework .util .ThreadSafe ;
2425import 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 );
Original file line number Diff line number Diff 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
512533class AccessProtectedBaseClass {
You can’t perform that action at this time.
0 commit comments