Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include::include.adoc[]

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

== 2.4 (2025-12-11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ default IStaticMock makeStaticMock(IMockCreationSettings settings) throws Cannot
* @param object a potential mock object
* @return information about the mock object or {@code null}
*/
@Nullable
default IMockObject asMockOrNull(Object object) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.spockframework.mock.ISpockMockObject;
import org.spockframework.mock.runtime.IMockMaker.IStaticMock;
import org.spockframework.mock.runtime.IMockMaker.MockMakerCapability;
import org.spockframework.runtime.GroovyRuntimeUtil;
import org.spockframework.util.InternalSpockError;
import org.spockframework.util.Nullable;
import org.spockframework.util.ThreadSafe;
Expand Down Expand Up @@ -182,7 +181,11 @@ private static void checkForStaticMockUsageWithClosure(IMockCreationSettings set
* @param object a mock object
* @return information about the mock object
*/
public IMockObject asMockOrNull(Object object) {
@Nullable
public IMockObject asMockOrNull(@Nullable Object object) {
if (object == null) {
return null;
}
if (object instanceof ISpockMockObject) {
return ((ISpockMockObject) object).$spock_get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.spockframework.mock.CannotCreateMockException;
import org.spockframework.mock.IMockObject;
import org.spockframework.mock.runtime.IMockMaker;
import org.spockframework.util.Nullable;
import org.spockframework.util.ReflectionUtil;
import org.spockframework.util.ThreadSafe;
import spock.mock.MockMakerId;
Expand Down Expand Up @@ -69,8 +70,9 @@ public int getPriority() {
}

@Override
public IMockObject asMockOrNull(Object object) {
if (impl == null) {
@Nullable
public IMockObject asMockOrNull(@Nullable Object object) {
if (impl == null || object == null) {
return null;
}
return impl.asMockOrNull(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class MockitoMockMakerSpec extends Specification {
mockito.toString() == "$ID default mock maker settings"
}

def "MockitoMockMaker.asMockOrNull() on null returns null"() {
given:
def maker = new MockitoMockMaker()

expect:
maker.asMockOrNull(null) == null
}

def "Verify ID and IMockMakerSettings with Mockito settings"() {
when:
def set = mockito {}
Expand Down Expand Up @@ -507,6 +515,19 @@ Can not mock final classes with the following settings :
additionalInterfaceClass.isInstance(m)
mockUtil.isMock(m)
}

@Issue("https://github.com/spockframework/spock/issues/2337")
def "NPE when stubbing method on null object"() {
given:
Object nullObj = null

when:
//Issue #2337: MockitoMockMaker passes null to Mockito's getHandler() throws NPE
nullObj.toString() >> ""

then:
noExceptionThrown()
}
}

class AccessProtectedBaseClass {
Expand Down
Loading