11package user .org .mockito
22
3- import org .mockito .{ArgumentMatchersSugar , IdiomaticMockito , VerifyOrder }
43import org .mockito .captor .ArgCaptor
54import org .mockito .exceptions .verification ._
65import org .mockito .invocation .InvocationOnMock
6+ import org .mockito .{ ArgumentMatchersSugar , IdiomaticMockito }
77import org .scalatest
88import org .scalatest .WordSpec
9-
10- import scala .language .postfixOps
9+ import user .org .mockito .matchers .{ ValueCaseClass , ValueClass }
1110
1211class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with IdiomaticMockito with ArgumentMatchersSugar {
1312
@@ -27,11 +26,15 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
2726
2827 def highOrderFunction (f : Int => String ): String = " not mocked"
2928
30- def iReturnAFunction (v : Int ): Int => String = i => i * v toString
29+ def iReturnAFunction (v : Int ): Int => String = i => ( i * v). toString
3130
3231 def iBlowUp (v : Int , v2 : String ): String = throw new IllegalArgumentException (" I was called!" )
3332
3433 def iHaveTypeParamsAndImplicits [A , B ](a : A , b : B )(implicit v3 : Implicit [A ]): String = " not mocked"
34+
35+ def valueClass (n : Int , v : ValueClass ): String = ???
36+
37+ def valueCaseClass (n : Int , v : ValueCaseClass ): String = ???
3538 }
3639
3740 class Bar {
@@ -58,7 +61,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
5861 }
5962
6063 " create a mock where I can mix matchers, normal and implicit parameters" in {
61- val aMock = mock[Foo ]
64+ val aMock = mock[Foo ]
6265 implicit val implicitValue : Implicit [Int ] = mock[Implicit [Int ]]
6366
6467 aMock.iHaveTypeParamsAndImplicits[Int , String ](* , " test" ) shouldReturn " mocked!"
@@ -73,7 +76,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
7376 " stub a real call" in {
7477 val aMock = mock[Foo ]
7578
76- aMock.bar shouldCallRealMethod
79+ aMock.bar shouldCall realMethod
7780
7881 aMock.bar shouldBe " not mocked"
7982 }
@@ -117,10 +120,10 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
117120 val aMock = mock[Foo ]
118121
119122 aMock.doSomethingWithThisInt(* ) shouldAnswer ((i : Int ) => i * 10 + 2 )
120- aMock.doSomethingWithThisIntAndString(* , * ) shouldAnswer ((i : Int , s : String ) => i * 10 + s.toInt toString)
123+ aMock.doSomethingWithThisIntAndString(* , * ) shouldAnswer ((i : Int , s : String ) => ( i * 10 + s.toInt). toString)
121124 aMock.doSomethingWithThisIntAndStringAndBoolean(* , * , * ) shouldAnswer ((i : Int ,
122125 s : String ,
123- boolean : Boolean ) => (i * 10 + s.toInt toString) + boolean)
126+ boolean : Boolean ) => (i * 10 + s.toInt).toString + boolean)
124127
125128 aMock.doSomethingWithThisInt(4 ) shouldBe 42
126129 aMock.doSomethingWithThisIntAndString(4 , " 2" ) shouldBe " 42"
@@ -167,7 +170,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
167170 " allow using less params than method on answer stubbing" in {
168171 val aMock = mock[Foo ]
169172
170- aMock.doSomethingWithThisIntAndStringAndBoolean(* , * , * ) shouldAnswer ((i : Int , s : String ) => i * 10 + s.toInt toString)
173+ aMock.doSomethingWithThisIntAndStringAndBoolean(* , * , * ) shouldAnswer ((i : Int , s : String ) => ( i * 10 + s.toInt). toString)
171174
172175 aMock.doSomethingWithThisIntAndStringAndBoolean(4 , " 2" , v3 = true ) shouldBe " 42"
173176 }
@@ -192,7 +195,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
192195 " stub a method that returns a function" in {
193196 val aMock = mock[Foo ]
194197
195- aMock.iReturnAFunction(* ) shouldReturn (_.toString) andThen (i => (i * 2 ) toString) andThenCallRealMethod ()
198+ aMock.iReturnAFunction(* ) shouldReturn (_.toString) andThen (i => (i * 2 ). toString) andThenCallRealMethod ()
196199
197200 aMock.iReturnAFunction(0 )(42 ) shouldBe " 42"
198201 aMock.iReturnAFunction(0 )(42 ) shouldBe " 84"
@@ -222,8 +225,8 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
222225 val aSpy = spy(new Foo )
223226
224227 ((i : Int ) => i * 10 + 2 ) willBe answered by aSpy.doSomethingWithThisInt(* )
225- ((i : Int , s : String ) => i * 10 + s.toInt toString) willBe answered by aSpy.doSomethingWithThisIntAndString(* , * )
226- ((i : Int , s : String , boolean : Boolean ) => (i * 10 + s.toInt toString) + boolean) willBe answered by aSpy
228+ ((i : Int , s : String ) => ( i * 10 + s.toInt). toString) willBe answered by aSpy.doSomethingWithThisIntAndString(* , * )
229+ ((i : Int , s : String , boolean : Boolean ) => (i * 10 + s.toInt).toString + boolean) willBe answered by aSpy
227230 .doSomethingWithThisIntAndStringAndBoolean(* , * , v3 = true )
228231 (() => " mocked!" ) willBe answered by aSpy.bar
229232 " mocked!" willBe answered by aSpy.baz
@@ -265,12 +268,13 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
265268 " check a mock was not used" in {
266269 val aMock = mock[Foo ]
267270
268- aMock was never called
271+ aMock wasNever called
272+ aMock wasNever called
269273
270274 a[NoInteractionsWanted ] should be thrownBy {
271275 aMock.baz
272276
273- aMock was never called
277+ aMock wasNever called
274278 }
275279 }
276280
@@ -279,12 +283,12 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
279283 }
280284
281285 " check a mock was not used (with setup)" in new SetupNeverUsed {
282- aMock was never called
286+ aMock wasNever called
283287
284288 a[NoInteractionsWanted ] should be thrownBy {
285289 aMock.baz
286290
287- aMock was never called
291+ aMock wasNever called
288292 }
289293 }
290294
@@ -314,15 +318,15 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
314318 }
315319 }
316320
317- " check a method was never called" in {
321+ " check a method wasNever called" in {
318322 val aMock = mock[Foo ]
319323
320- aMock.doSomethingWithThisIntAndString(* , " test" ) was never called
324+ aMock.doSomethingWithThisIntAndString(* , " test" ) wasNever called
321325
322326 a[NeverWantedButInvoked ] should be thrownBy {
323327 aMock.doSomethingWithThisIntAndString(1 , " test" )
324328
325- aMock.doSomethingWithThisIntAndString(* , " test" ) was never called
329+ aMock.doSomethingWithThisIntAndString(* , " test" ) wasNever called
326330 }
327331 }
328332
@@ -385,12 +389,12 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
385389
386390 aMock.bar was called
387391
388- aMock was never called again
392+ aMock wasNever calledAgain
389393
390394 a[NoInteractionsWanted ] should be thrownBy {
391395 aMock.bar
392396
393- aMock was never called again
397+ aMock wasNever calledAgain
394398 }
395399 }
396400
@@ -429,4 +433,35 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
429433 }
430434 }
431435 }
436+
437+ " value class matchers" should {
438+ " eqToVal works with new syntax" in {
439+ val aMock = mock[Foo ]
440+
441+ aMock.valueClass(1 , eqToVal(new ValueClass (" meh" ))) shouldReturn " mocked!"
442+ aMock.valueClass(1 , new ValueClass (" meh" )) shouldBe " mocked!"
443+ aMock.valueClass(1 , eqToVal(new ValueClass (" meh" ))) was called
444+
445+ aMock.valueCaseClass(2 , eqToVal(ValueCaseClass (100 ))) shouldReturn " mocked!"
446+ aMock.valueCaseClass(2 , ValueCaseClass (100 )) shouldBe " mocked!"
447+ aMock.valueCaseClass(2 , eqToVal(ValueCaseClass (100 ))) was called
448+
449+ val value = ValueCaseClass (100 )
450+ aMock.valueCaseClass(3 , eqToVal(value)) shouldReturn " mocked!"
451+ aMock.valueCaseClass(3 , ValueCaseClass (100 )) shouldBe " mocked!"
452+ aMock.valueCaseClass(3 , eqToVal(value)) was called
453+ }
454+
455+ " anyVal works with new syntax" in {
456+ val aMock = mock[Foo ]
457+
458+ aMock.valueClass(1 , anyVal[ValueClass ]) shouldReturn " mocked!"
459+ aMock.valueClass(1 , new ValueClass (" meh" )) shouldBe " mocked!"
460+ aMock.valueClass(1 , anyVal[ValueClass ]) was called
461+
462+ aMock.valueCaseClass(2 , anyVal[ValueCaseClass ]) shouldReturn " mocked!"
463+ aMock.valueCaseClass(2 , ValueCaseClass (100 )) shouldBe " mocked!"
464+ aMock.valueCaseClass(2 , anyVal[ValueCaseClass ]) was called
465+ }
466+ }
432467}
0 commit comments