Skip to content

Commit 726d9ec

Browse files
authored
Improve varargs handling (#353)
1 parent 11ad429 commit 726d9ec

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

common/src/main/scala/org/mockito/internal/handler/ScalaMockHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ScalaMockHandler[T](mockSettings: MockCreationSettings[T], methodsToProces
7777
.getOrElse(args)
7878

7979
//For some border cases, we can't extract the varargs in the nice way, so we try the brute force one
80-
if (args.length != transformed.length) transformed
80+
if (methodsToProcess.isEmpty || args.length != transformed.length) transformed
8181
else unwrapVarargs(transformed)
8282
}
8383
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package user.org.mockito
2+
3+
import org.mockito.scalatest.MockitoSugar
4+
import org.scalatest.funspec.AnyFunSpec
5+
import org.scalatest.matchers.should.Matchers
6+
7+
import scala.collection.immutable.ArraySeq
8+
9+
class Issue352_213 extends AnyFunSpec with Matchers with MockitoSugar {
10+
11+
trait IndexedSeqService {
12+
def testMethod(str: String, args: IndexedSeq[String]): IndexedSeq[String]
13+
}
14+
trait ArraySeqService {
15+
def testMethod(str: String, args: ArraySeq[String]): IndexedSeq[String]
16+
}
17+
18+
describe("Mockito") {
19+
it("should allow ArraySeq as a last parameter") {
20+
val arraySeq = mock[ArraySeqService]
21+
when(arraySeq.testMethod(*, *)).thenAnswer[String, ArraySeq[String]] { (fst, rst) =>
22+
fst +: rst
23+
}
24+
arraySeq.testMethod("a", ArraySeq("b", "c")).should(contain).allOf("a", "b", "c")
25+
}
26+
it("should allow ArraySeq as a last parameter to IndexedSeq method") {
27+
val indexedSeq = mock[IndexedSeqService]
28+
when(indexedSeq.testMethod(*, *)).thenAnswer[String, IndexedSeq[String]] { (fst, rst) =>
29+
fst +: rst
30+
}
31+
indexedSeq.testMethod("a", ArraySeq("b", "c")).should(contain).allOf("a", "b", "c")
32+
}
33+
}
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package user.org.mockito
2+
3+
import org.mockito.scalatest.MockitoSugar
4+
import org.scalatest.funspec.AnyFunSpec
5+
import org.scalatest.matchers.should.Matchers
6+
7+
class Issue352 extends AnyFunSpec with Matchers with MockitoSugar {
8+
9+
trait IndexedSeqService {
10+
def testMethod(str: String, args: IndexedSeq[String]): IndexedSeq[String]
11+
}
12+
trait VarArgsService {
13+
def testMethod(str: String, args: String*): IndexedSeq[String]
14+
}
15+
16+
describe("Mockito") {
17+
it("should allow Vector as a last parameter to IndexedSeq method") {
18+
val indexedSeq = mock[IndexedSeqService]
19+
when(indexedSeq.testMethod(*, *)).thenAnswer[String, IndexedSeq[String]] { (fst, rst) =>
20+
fst +: rst
21+
}
22+
indexedSeq.testMethod("a", Vector("b", "c")).should(contain).allOf("a", "b", "c")
23+
}
24+
it("should allow varargs as a last parameter to IndexedSeq method 2 varargs") {
25+
val varargSeq = mock[VarArgsService]
26+
when(varargSeq.testMethod(*, *)).thenAnswer[String, String, String] { (v0, v1, v2) =>
27+
IndexedSeq(v0, v1, v2)
28+
}
29+
varargSeq.testMethod("a", "b", "c").should(contain).allOf("a", "b", "c")
30+
}
31+
it("should allow varargs as a last parameter to IndexedSeq method 1 vararg") {
32+
val varargSeq = mock[VarArgsService]
33+
when(varargSeq.testMethod(*, *)).thenAnswer[String, String] { (v0, v1) =>
34+
IndexedSeq(v0, v1)
35+
}
36+
varargSeq.testMethod("a", "b").should(contain).allOf("a", "b")
37+
}
38+
it("should allow varargs as a last parameter to IndexedSeq method no vararg") {
39+
val varargSeq = mock[VarArgsService]
40+
when(varargSeq.testMethod(*, *)).thenAnswer[String] { v0 =>
41+
IndexedSeq(v0)
42+
}
43+
varargSeq.testMethod("a").should(contain).only("a")
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)