|
1 | 1 | package user.org.mockito
|
2 | 2 |
|
3 |
| -import org.mockito.{ArgumentMatchersSugar, MockitoSugar} |
4 |
| -import org.scalatest.{Matchers, WordSpec} |
| 3 | +import org.mockito.{ ArgumentMatchersSugar, MockitoSugar } |
| 4 | +import org.scalatest.prop.TableDrivenPropertyChecks |
| 5 | +import org.scalatest.{ Matchers, WordSpec } |
5 | 6 |
|
6 | 7 | //noinspection RedundantDefaultArgument
|
7 |
| -class MockitoSugarTest_212 extends WordSpec with MockitoSugar with Matchers with ArgumentMatchersSugar { |
| 8 | +class MockitoSugarTest_212 extends WordSpec with MockitoSugar with Matchers with ArgumentMatchersSugar with TableDrivenPropertyChecks { |
8 | 9 |
|
9 |
| - class Foo { |
10 |
| - def bar = "not mocked" |
| 10 | + val scenarios = Table( |
| 11 | + ("testDouble", "foo", "baz"), |
| 12 | + ("mock", () => mock[Foo], () => mock[Baz]), |
| 13 | + ("spy", () => spy(new Foo), () => spy(new ConcreteBaz)) |
| 14 | + ) |
11 | 15 |
|
12 |
| - def iHaveByNameArgs(normal: String, byName: => String, byName2: => String): String = ??? |
| 16 | + forAll(scenarios) { (testDouble, foo, baz) => |
| 17 | + testDouble should { |
13 | 18 |
|
14 |
| - def iHaveByNameAndFunction0Args(normal: String, f0: () => String, byName: => String): String = ??? |
15 |
| - } |
16 |
| - |
17 |
| - trait Baz { |
18 |
| - def traitMethod(defaultArg: Int = 30, anotherDefault: String = "hola"): Int = ??? |
19 |
| - } |
20 |
| - |
21 |
| - class SomeClass extends Foo with Baz |
| 19 | + "work with default arguments in traits" in { |
| 20 | + val testDouble = baz() |
22 | 21 |
|
23 |
| - "mock[T]" should { |
| 22 | + when(testDouble.traitMethodWithDefaultArgs(any, any)) thenReturn 69 |
24 | 23 |
|
25 |
| - "work with default arguments in traits" in { |
26 |
| - val aMock = mock[Foo with Baz] |
| 24 | + testDouble.traitMethodWithDefaultArgs() shouldBe 69 |
27 | 25 |
|
28 |
| - when(aMock.bar) thenReturn "mocked!" |
29 |
| - when(aMock.traitMethod(any, any)) thenReturn 69 |
| 26 | + verify(testDouble).traitMethodWithDefaultArgs(30, "hola") |
| 27 | + } |
30 | 28 |
|
31 |
| - aMock.bar shouldBe "mocked!" |
32 |
| - aMock.traitMethod() shouldBe 69 |
| 29 | + "work with by-name arguments and matchers (by-name arguments have to be the last ones when using matchers)" in { |
| 30 | + val testDouble = foo() |
33 | 31 |
|
34 |
| - verify(aMock).traitMethod(30, "hola") |
35 |
| - } |
36 |
| - |
37 |
| - "work with by-name arguments and matchers (by-name arguments have to be the last ones when using matchers)" in { |
38 |
| - val aMock = mock[Foo] |
| 32 | + when(testDouble.iHaveByNameArgs(any, any, any)) thenReturn "mocked!" |
39 | 33 |
|
40 |
| - when(aMock.iHaveByNameArgs(any, any, any)) thenReturn "mocked!" |
| 34 | + testDouble.iHaveByNameArgs("arg1", "arg2", "arg3") shouldBe "mocked!" |
41 | 35 |
|
42 |
| - aMock.iHaveByNameArgs("arg1", "arg2", "arg3") shouldBe "mocked!" |
43 |
| - |
44 |
| - verify(aMock).iHaveByNameArgs(eqTo("arg1"), endsWith("g2"), eqTo("arg3")) |
45 |
| - } |
| 36 | + verify(testDouble).iHaveByNameArgs(eqTo("arg1"), endsWith("g2"), eqTo("arg3")) |
| 37 | + } |
46 | 38 |
|
47 |
| - "work with by-name and Function0 arguments (by-name arguments have to be the last ones when using matchers)" in { |
48 |
| - val aMock = mock[Foo] |
| 39 | + "work with by-name and Function0 arguments (by-name arguments have to be the last ones when using matchers)" in { |
| 40 | + val testDouble = foo() |
49 | 41 |
|
50 |
| - when(aMock.iHaveByNameAndFunction0Args(any, any, any)) thenReturn "mocked!" |
| 42 | + when(testDouble.iHaveByNameAndFunction0Args(any, any, any)) thenReturn "mocked!" |
51 | 43 |
|
52 |
| - aMock.iHaveByNameAndFunction0Args("arg1", () => "arg2", "arg3") shouldBe "mocked!" |
| 44 | + testDouble.iHaveByNameAndFunction0Args("arg1", () => "arg2", "arg3") shouldBe "mocked!" |
53 | 45 |
|
54 |
| - verify(aMock).iHaveByNameAndFunction0Args(eqTo("arg1"), function0("arg2"), startsWith("arg")) |
| 46 | + verify(testDouble).iHaveByNameAndFunction0Args(eqTo("arg1"), function0("arg2"), startsWith("arg")) |
| 47 | + } |
55 | 48 | }
|
56 | 49 | }
|
57 | 50 | }
|
0 commit comments