Skip to content

Commit 52ad7b1

Browse files
beatbrotAndreasTu
andauthored
Avoid duplicate vararg handling with Mockito (#2201)
As @meestchoo correctly suspected in #2161, vararg expansion in conjunction with the Mockito MockMaker is handled by both Mockito and Spock. The easy fix is to just not use Mockito var expansion. Fixes #2161 --------- Co-authored-by: AndreasTu <[email protected]>
1 parent a602687 commit 52ad7b1

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docs/release_notes.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ spockPull:1910[]
5555
will now require that the spec is annotated with <<parallel_execution.adoc#isolated-execution, @Isolated>> or `@ResourceLock(org.spockframework.runtime.model.parallel.Resources.META_CLASS_REGISTRY)`. See <<interaction_based_testing.adoc#global-mocks-parallel-execution, Global mocks and parallel execution>> spockPull:1848[]
5656
* `@TempDir` `spock.tempDir.keep` has been replaced by `spock.tempdir.cleanup`. See <<extensions.adoc#temp-dir-cleanup,TempDir Cleanup>> spockPull:1525[]
5757

58+
=== Misc
59+
60+
* Fix vararg handling in SpyStatic spockIssue:2161[]
61+
5862
== 2.4-M6 (2025-04-15)
5963

6064
=== Highlights

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public SpockMockHandler(IProxyBasedMockInterceptor mockInterceptor) {
262262
@Override
263263
public Object handle(Invocation invocation) {
264264
Object mock = invocation.getMock();
265-
return mockInterceptor.intercept(mock, invocation.getMethod(), invocation.getArguments(), new IResponseGenerator() {
265+
return mockInterceptor.intercept(mock, invocation.getMethod(), invocation.getRawArguments(), new IResponseGenerator() {
266266
@Override
267267
public Object respond(IMockInvocation __) {
268268
try {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.mockito.exceptions.base.MockitoException
2222
import org.spockframework.mock.CannotCreateMockException
2323
import org.spockframework.mock.MockUtil
2424
import org.spockframework.runtime.InvalidSpecException
25+
import spock.lang.Issue
2526
import spock.lang.Shared
2627
import spock.lang.Specification
2728
import spock.mock.MockMakers
@@ -478,6 +479,18 @@ class MockitoStaticMocksSpec extends Specification {
478479
ex.message.contains("It is not possible to mock static methods of java.lang.Thread")
479480
}
480481

482+
@Issue("https://github.com/spockframework/spock/issues/2161")
483+
def "SpyStatic with varargs in method"() {
484+
given:
485+
SpyStatic(StaticClass)
486+
StaticClass.staticVarargsMethod("test") >> true
487+
StaticClass.staticVarargsMethod("test2") >> false
488+
489+
expect:
490+
StaticClass.staticVarargsMethod("test")
491+
!StaticClass.staticVarargsMethod("test2")
492+
}
493+
481494
static class StaticClass {
482495

483496
String instanceMethod() {
@@ -491,6 +504,11 @@ class MockitoStaticMocksSpec extends Specification {
491504
static String staticMethod2() {
492505
return REAL_VALUE
493506
}
507+
508+
@SuppressWarnings('unused')
509+
static boolean staticVarargsMethod(String str, String... varargs) {
510+
return true
511+
}
494512
}
495513

496514
static class StaticClass2 {

0 commit comments

Comments
 (0)