forked from mockito/mockito-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdiomaticStubbingTest.scala
More file actions
461 lines (336 loc) · 15.3 KB
/
Copy pathIdiomaticStubbingTest.scala
File metadata and controls
461 lines (336 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package user.org.mockito
import java.util.concurrent.atomic.AtomicInteger
import org.mockito.invocation.InvocationOnMock
import org.mockito.*
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import user.org.mockito.matchers.{ ValueCaseClassInt, ValueCaseClassString, ValueClass }
import scala.collection.parallel.immutable
trait PolymorphicCodec[A]
case class PolymorphicResponse[A](body: A)
trait PolymorphicClient {
def request[A](path: String)(implicit codec: PolymorphicCodec[A]): Either[String, PolymorphicResponse[A]]
}
class IdiomaticStubbingTest extends AnyWordSpec with Matchers with ArgumentMatchersSugar with IdiomaticMockitoTestSetup with IdiomaticStubbing {
forAll(scenarios) { (testDouble, orgDouble, foo) =>
testDouble should {
"stub a return value" in {
val org = orgDouble()
org.bar returns "mocked!"
org.bar shouldBe "mocked!"
}
"stub a value class return value" in {
val org = orgDouble()
org.returnsValueCaseClassInt returns ValueCaseClassInt(100) andThen ValueCaseClassInt(200)
org.returnsValueCaseClassInt shouldBe ValueCaseClassInt(100)
org.returnsValueCaseClassInt shouldBe ValueCaseClassInt(200)
}
"stub multiple return values" in {
val org = orgDouble()
org.bar returns "mocked!" andThen "mocked again!"
org.bar shouldBe "mocked!"
org.bar shouldBe "mocked again!"
org.bar shouldBe "mocked again!"
}
"stub an exception instance to be thrown" in {
val org = orgDouble()
org.bar throws new IllegalArgumentException
an[IllegalArgumentException] shouldBe thrownBy(org.bar)
}
"chain exception and value" in {
val org = orgDouble()
org.bar throws new IllegalArgumentException andThen "mocked!"
an[IllegalArgumentException] shouldBe thrownBy(org.bar)
org.bar shouldBe "mocked!"
}
"chain value and exception" in {
val org = orgDouble()
org.bar returns "mocked!" andThenThrow new IllegalArgumentException
org.bar shouldBe "mocked!"
an[IllegalArgumentException] shouldBe thrownBy(org.bar)
}
// useful if we want to delay the evaluation of whatever we are returning until the method is called
"simplify stubbing an answer where we don't care about any param" in {
val org = orgDouble()
val counter = new AtomicInteger(1)
org.bar answers counter.getAndIncrement().toString
counter.get shouldBe 1
org.bar shouldBe "1"
counter.get shouldBe 2
org.bar shouldBe "2"
}
"simplify answer API" in {
val org = orgDouble()
org.doSomethingWithThisInt(*) answers ((i: Int) => i * 10 + 2)
org.doSomethingWithThisIntAndString(*, *) answers ((i: Int, s: String) => (i * 10 + s.toInt).toString)
org.doSomethingWithThisIntAndStringAndBoolean(*, *, *) answers ((i: Int, s: String, boolean: Boolean) => (i * 10 + s.toInt).toString + boolean)
org.doSomethingWithThisInt(4) shouldBe 42
org.doSomethingWithThisIntAndString(4, "2") shouldBe "42"
org.doSomethingWithThisIntAndStringAndBoolean(4, "2", v3 = true) shouldBe "42true"
}
"create a mock where I can mix matchers and normal parameters (answer)" in {
val org = orgDouble()
org.doSomethingWithThisIntAndString(*, "test") answers "mocked!"
org.doSomethingWithThisIntAndString(3, "test") shouldBe "mocked!"
org.doSomethingWithThisIntAndString(5, "test") shouldBe "mocked!"
org.doSomethingWithThisIntAndString(5, "est") should not be "mocked!"
}
"simplify answer API (invocation usage)" in {
val org = orgDouble()
org.doSomethingWithThisInt(*) answers ((i: InvocationOnMock) => i.arg[Int](0) * 10 + 2)
org.doSomethingWithThisInt(4) shouldBe 42
}
"chain answers" in {
val org = orgDouble()
org.doSomethingWithThisInt(*) answers ((i: Int) => i * 10 + 2) andThenAnswer ((i: Int) => i * 15 + 9)
org.doSomethingWithThisInt(4) shouldBe 42
org.doSomethingWithThisInt(4) shouldBe 69
}
"chain answers (invocation usage)" in {
val org = orgDouble()
org.doSomethingWithThisInt(*) answers ((i: InvocationOnMock) => i.arg[Int](0) * 10 + 2) andThenAnswer ((i: InvocationOnMock) => i.arg[Int](0) * 15 + 9)
org.doSomethingWithThisInt(4) shouldBe 42
org.doSomethingWithThisInt(4) shouldBe 69
}
"allow using less params than method on answer stubbing" in {
val org = orgDouble()
org.doSomethingWithThisIntAndStringAndBoolean(*, *, *) answers ((i: Int, s: String) => (i * 10 + s.toInt).toString)
org.doSomethingWithThisIntAndStringAndBoolean(4, "2", v3 = true) shouldBe "42"
}
"stub a mock inline that has default args" in {
val aMock = orgDouble()
aMock.returnBar returns mock[Bar] andThen mock[Bar]
aMock.returnBar shouldBe a[Bar]
aMock.returnBar shouldBe a[Bar]
}
"stub a null reference return value (infix)" in {
val aMock = orgDouble()
aMock.returnBar returns null
aMock.returnBar shouldBe null
}
"stub a null String return value" in {
val aMock = orgDouble()
aMock.bar returns null
aMock.bar shouldBe null
}
"stub a null String return value via answers" in {
val aMock = orgDouble()
aMock.bar answers (null: String)
aMock.bar shouldBe null
}
"stub a high order function" in {
val org = orgDouble()
org.highOrderFunction(*) returns "mocked!"
org.highOrderFunction(_.toString) shouldBe "mocked!"
}
"stub a method that returns a function" in {
val org = orgDouble()
org.iReturnAFunction(*).shouldReturn(_.toString).andThen(i => (i * 2).toString).andThenCallRealMethod()
org.iReturnAFunction(0)(42) shouldBe "42"
org.iReturnAFunction(0)(42) shouldBe "84"
org.iReturnAFunction(3)(3) shouldBe "9"
}
"doStub a value class return value" in {
val org = orgDouble()
ValueCaseClassString("100") willBe returned by org.returnsValueCaseClassString
ValueCaseClassInt(100) willBe returned by org.returnsValueCaseClassInt
org.returnsValueCaseClassString shouldBe ValueCaseClassString("100")
org.returnsValueCaseClassInt shouldBe ValueCaseClassInt(100)
}
"doStub a takes value classes" in {
val org = orgDouble()
{ (v: ValueClass, v1: ValueCaseClassInt, v2: ValueCaseClassString) =>
s"$v-$v1-$v2"
} willBe answered by org.takesManyValueClasses(any[ValueClass], any[ValueCaseClassInt], any[ValueCaseClassString])
org.takesManyValueClasses(new ValueClass("1"), ValueCaseClassInt(2), ValueCaseClassString("3")) shouldBe "ValueClass(1)-ValueCaseClassInt(2)-ValueCaseClassString(3)"
}
"doStub return value should be type safe" in {
val org = orgDouble()
ValueCaseClassInt(100) willBe returned by org.returnsValueCaseClassInt
""""mocked" willBe returned by org.returnsValueCaseClass""" shouldNot compile
}
"doStub return value should be type safe and allow subtypes" in {
val org = orgDouble()
Some("Hola") willBe returned by org.option
None willBe returned by org.option
"""Some(42) willBe returned by org.option""" shouldNot compile
}
"doStub answer value should be type safe" in {
val org = orgDouble()
{ (v1: Int, _: String) =>
v1.toString
} willBe answered by org.doSomethingWithThisIntAndStringAndBoolean(*, *, v3 = true)
{ (_: Int, v2: String) =>
v2
} willBe answered by org.doSomethingWithThisIntAndStringAndBoolean(*, *, v3 = true)
"""{ (_: Int, _: String, v3: Boolean) => v3 } willBe answered by org.doSomethingWithThisIntAndStringAndBoolean(*, *, v3 = true)""" shouldNot compile
}
"doStub answer function should be type safe and allow subtypes" in {
val org = orgDouble()
{ (a: String, _: Int) =>
Some(a)
} willBe answered by org.option(*, *)
{ (_: String, _: Int) =>
None: Option[String]
} willBe answered by org.option(*, *)
"""{ (a: String, b: Int) => Some(b) } willBe answered by org.option2(*, *)""" shouldNot compile
}
"doStub a failure" in {
val org = orgDouble()
new IllegalArgumentException willBe thrown by org.doSomethingWithThisIntAndStringAndBoolean(*, *, v3 = true)
org.doSomethingWithThisIntAndStringAndBoolean(1, "2", v3 = false)
an[IllegalArgumentException] should be thrownBy
org.doSomethingWithThisIntAndStringAndBoolean(1, "2", v3 = true)
""""some value" willBe thrown by org.bar""" shouldNot compile
}
}
}
"spy" should {
"stub a function that would fail if the real impl is called" in {
val aSpy = spy(new Org)
an[IllegalArgumentException] should be thrownBy {
aSpy.iBlowUp(*, *) returns "mocked!"
}
"mocked!" willBe returned by aSpy.iBlowUp(*, "ok")
aSpy.iBlowUp(1, "ok") shouldBe "mocked!"
aSpy.iBlowUp(2, "ok") shouldBe "mocked!"
an[IllegalArgumentException] should be thrownBy
aSpy.iBlowUp(2, "not ok")
}
"stub a function with an answer" in {
val aSpy = spy(new Org)
((i: Int) => i * 10 + 2) willBe answered by aSpy.doSomethingWithThisInt(*)
((i: Int, s: String) => (i * 10 + s.toInt).toString) willBe answered by aSpy.doSomethingWithThisIntAndString(*, *)
((i: Int, s: String, boolean: Boolean) => (i * 10 + s.toInt).toString + boolean) willBe answered by aSpy
.doSomethingWithThisIntAndStringAndBoolean(*, *, v3 = true)
val counter = new AtomicInteger(1)
(() => counter.getAndIncrement().toString) willBe answered by aSpy.bar
counter.getAndIncrement().toString willBe answered by aSpy.baz
counter.get shouldBe 1
aSpy.bar shouldBe "1"
counter.get shouldBe 2
aSpy.baz shouldBe "2"
counter.get shouldBe 3
aSpy.doSomethingWithThisInt(4) shouldBe 42
aSpy.doSomethingWithThisIntAndString(4, "2") shouldBe "42"
aSpy.doSomethingWithThisIntAndStringAndBoolean(4, "2", v3 = true) shouldBe "42true"
aSpy.doSomethingWithThisIntAndStringAndBoolean(4, "2", v3 = false) shouldBe "not mocked"
}
"stub a real call" in {
val org = mock[Org]
theRealMethod willBe called by org.doSomethingWithThisIntAndStringAndBoolean(*, *, v3 = true)
org.doSomethingWithThisIntAndStringAndBoolean(1, "2", v3 = true) shouldBe "not mocked"
org.doSomethingWithThisIntAndStringAndBoolean(1, "2", v3 = false) shouldBe ""
}
"stub an object method" in {
FooObject.simpleMethod shouldBe "not mocked!"
withObjectSpied[FooObject.type] {
FooObject.simpleMethod returns "spied!"
FooObject.simpleMethod shouldBe "spied!"
}
FooObject.simpleMethod shouldBe "not mocked!"
}
"call real object method when not stubbed" in {
val now = FooObject.stateDependantMethod
withObjectSpied[FooObject.type] {
FooObject.simpleMethod returns s"spied!"
FooObject.simpleMethod shouldBe s"spied!"
FooObject.stateDependantMethod shouldBe now
}
}
"be thread safe" when {
"always stubbing object methods" in
immutable.ParSeq.range(1, 100).foreach { i =>
withObjectSpied[FooObject.type] {
FooObject.simpleMethod returns s"spied!-$i"
FooObject.simpleMethod shouldBe s"spied!-$i"
}
}
"intermittently stubbing object methods" in {
val now = FooObject.stateDependantMethod
immutable.ParSeq.range(1, 100).foreach { i =>
if (i % 2 == 0)
withObjectSpied[FooObject.type] {
FooObject.stateDependantMethod returns i
FooObject.stateDependantMethod shouldBe i
}
else FooObject.stateDependantMethod shouldBe now
}
}
}
}
"mock" should {
"infer the type parameter for shouldReturn on a polymorphic method from the returned value" in {
implicit object StringCodec extends PolymorphicCodec[String]
def stubResponse[A](
client: PolymorphicClient,
response: Either[String, PolymorphicResponse[A]]
)(implicit codec: PolymorphicCodec[A]): org.mockito.stubbing.ScalaOngoingStubbing[Either[String, PolymorphicResponse[A]]] =
client.request("path")(*) shouldReturn response
val client = mock[PolymorphicClient]
val expected = Right(PolymorphicResponse("ok")): Either[String, PolymorphicResponse[String]]
stubResponse(client, expected)
client.request[String]("path") shouldBe expected
}
"infer the type parameter for mustReturn on a polymorphic method from the returned value" in {
implicit object StringCodec extends PolymorphicCodec[String]
def stubResponse[A](
client: PolymorphicClient,
response: Either[String, PolymorphicResponse[A]]
)(implicit codec: PolymorphicCodec[A]): org.mockito.stubbing.ScalaOngoingStubbing[Either[String, PolymorphicResponse[A]]] =
client.request("path")(*) mustReturn response
val client = mock[PolymorphicClient]
val expected = Right(PolymorphicResponse("ok")): Either[String, PolymorphicResponse[String]]
stubResponse(client, expected)
client.request[String]("path") shouldBe expected
}
"stub a map" in {
val mocked = mock[Map[String, String]]
mocked(*) returns "123"
mocked("key") shouldBe "123"
}
"stub an object method" in {
FooObject.simpleMethod shouldBe "not mocked!"
withObjectMocked[FooObject.type] {
FooObject.simpleMethod returns "mocked!"
FooObject.simpleMethod shouldBe "mocked!"
}
FooObject.simpleMethod shouldBe "not mocked!"
}
"object stubbing should be thread safe" in
immutable.ParSeq.range(1, 100).foreach { i =>
withObjectMocked[FooObject.type] {
FooObject.simpleMethod returns s"mocked!-$i"
FooObject.simpleMethod shouldBe s"mocked!-$i"
}
}
"object stubbing should be thread safe 2" in {
val now = FooObject.stateDependantMethod
immutable.ParSeq.range(1, 100).foreach { i =>
if (i % 2 == 0)
withObjectMocked[FooObject.type] {
FooObject.stateDependantMethod returns i
FooObject.stateDependantMethod shouldBe i
}
else FooObject.stateDependantMethod shouldBe now
}
}
"stub methods from traits in object" in {
ObjectWithTraits.methodFromTraitA shouldBe "TraitA implementation"
ObjectWithTraits.methodFromTraitB shouldBe 42
withObjectMocked[ObjectWithTraits.type] {
// Verify the mock implements the trait interfaces
ObjectWithTraits shouldBe a[TraitA]
ObjectWithTraits shouldBe a[TraitB]
ObjectWithTraits.methodFromTraitA returns "mocked A"
ObjectWithTraits.methodFromTraitB returns 99
ObjectWithTraits.ownMethod returns "mocked own"
ObjectWithTraits.methodFromTraitA shouldBe "mocked A"
ObjectWithTraits.methodFromTraitB shouldBe 99
ObjectWithTraits.ownMethod shouldBe "mocked own"
}
ObjectWithTraits.methodFromTraitA shouldBe "TraitA implementation"
ObjectWithTraits.methodFromTraitB shouldBe 42
}
}
}