1
1
package user .org .mockito
2
2
3
- import org .mockito .{ArgumentMatchersSugar , IdiomaticMockito , VerifyOrder }
4
3
import org .mockito .captor .ArgCaptor
5
4
import org .mockito .exceptions .verification ._
6
5
import org .mockito .invocation .InvocationOnMock
6
+ import org .mockito .{ ArgumentMatchersSugar , IdiomaticMockito }
7
7
import org .scalatest
8
8
import org .scalatest .WordSpec
9
-
10
- import scala .language .postfixOps
9
+ import user .org .mockito .matchers .{ ValueCaseClass , ValueClass }
11
10
12
11
class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with IdiomaticMockito with ArgumentMatchersSugar {
13
12
@@ -27,11 +26,15 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
27
26
28
27
def highOrderFunction (f : Int => String ): String = " not mocked"
29
28
30
- def iReturnAFunction (v : Int ): Int => String = i => i * v toString
29
+ def iReturnAFunction (v : Int ): Int => String = i => ( i * v). toString
31
30
32
31
def iBlowUp (v : Int , v2 : String ): String = throw new IllegalArgumentException (" I was called!" )
33
32
34
33
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 = ???
35
38
}
36
39
37
40
class Bar {
@@ -58,7 +61,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
58
61
}
59
62
60
63
" create a mock where I can mix matchers, normal and implicit parameters" in {
61
- val aMock = mock[Foo ]
64
+ val aMock = mock[Foo ]
62
65
implicit val implicitValue : Implicit [Int ] = mock[Implicit [Int ]]
63
66
64
67
aMock.iHaveTypeParamsAndImplicits[Int , String ](* , " test" ) shouldReturn " mocked!"
@@ -73,7 +76,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
73
76
" stub a real call" in {
74
77
val aMock = mock[Foo ]
75
78
76
- aMock.bar shouldCallRealMethod
79
+ aMock.bar shouldCall realMethod
77
80
78
81
aMock.bar shouldBe " not mocked"
79
82
}
@@ -117,10 +120,10 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
117
120
val aMock = mock[Foo ]
118
121
119
122
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)
121
124
aMock.doSomethingWithThisIntAndStringAndBoolean(* , * , * ) shouldAnswer ((i : Int ,
122
125
s : String ,
123
- boolean : Boolean ) => (i * 10 + s.toInt toString) + boolean)
126
+ boolean : Boolean ) => (i * 10 + s.toInt).toString + boolean)
124
127
125
128
aMock.doSomethingWithThisInt(4 ) shouldBe 42
126
129
aMock.doSomethingWithThisIntAndString(4 , " 2" ) shouldBe " 42"
@@ -167,7 +170,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
167
170
" allow using less params than method on answer stubbing" in {
168
171
val aMock = mock[Foo ]
169
172
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)
171
174
172
175
aMock.doSomethingWithThisIntAndStringAndBoolean(4 , " 2" , v3 = true ) shouldBe " 42"
173
176
}
@@ -192,7 +195,7 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
192
195
" stub a method that returns a function" in {
193
196
val aMock = mock[Foo ]
194
197
195
- aMock.iReturnAFunction(* ) shouldReturn (_.toString) andThen (i => (i * 2 ) toString) andThenCallRealMethod ()
198
+ aMock.iReturnAFunction(* ) shouldReturn (_.toString) andThen (i => (i * 2 ). toString) andThenCallRealMethod ()
196
199
197
200
aMock.iReturnAFunction(0 )(42 ) shouldBe " 42"
198
201
aMock.iReturnAFunction(0 )(42 ) shouldBe " 84"
@@ -222,8 +225,8 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
222
225
val aSpy = spy(new Foo )
223
226
224
227
((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
227
230
.doSomethingWithThisIntAndStringAndBoolean(* , * , v3 = true )
228
231
(() => " mocked!" ) willBe answered by aSpy.bar
229
232
" mocked!" willBe answered by aSpy.baz
@@ -265,12 +268,13 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
265
268
" check a mock was not used" in {
266
269
val aMock = mock[Foo ]
267
270
268
- aMock was never called
271
+ aMock wasNever called
272
+ aMock wasNever called
269
273
270
274
a[NoInteractionsWanted ] should be thrownBy {
271
275
aMock.baz
272
276
273
- aMock was never called
277
+ aMock wasNever called
274
278
}
275
279
}
276
280
@@ -279,12 +283,12 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
279
283
}
280
284
281
285
" check a mock was not used (with setup)" in new SetupNeverUsed {
282
- aMock was never called
286
+ aMock wasNever called
283
287
284
288
a[NoInteractionsWanted ] should be thrownBy {
285
289
aMock.baz
286
290
287
- aMock was never called
291
+ aMock wasNever called
288
292
}
289
293
}
290
294
@@ -314,15 +318,15 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
314
318
}
315
319
}
316
320
317
- " check a method was never called" in {
321
+ " check a method wasNever called" in {
318
322
val aMock = mock[Foo ]
319
323
320
- aMock.doSomethingWithThisIntAndString(* , " test" ) was never called
324
+ aMock.doSomethingWithThisIntAndString(* , " test" ) wasNever called
321
325
322
326
a[NeverWantedButInvoked ] should be thrownBy {
323
327
aMock.doSomethingWithThisIntAndString(1 , " test" )
324
328
325
- aMock.doSomethingWithThisIntAndString(* , " test" ) was never called
329
+ aMock.doSomethingWithThisIntAndString(* , " test" ) wasNever called
326
330
}
327
331
}
328
332
@@ -385,12 +389,12 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
385
389
386
390
aMock.bar was called
387
391
388
- aMock was never called again
392
+ aMock wasNever calledAgain
389
393
390
394
a[NoInteractionsWanted ] should be thrownBy {
391
395
aMock.bar
392
396
393
- aMock was never called again
397
+ aMock wasNever calledAgain
394
398
}
395
399
}
396
400
@@ -429,4 +433,35 @@ class IdiomaticMockitoTest extends WordSpec with scalatest.Matchers with Idiomat
429
433
}
430
434
}
431
435
}
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
+ }
432
467
}
0 commit comments