-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
AnnotationsTest.kt
144 lines (105 loc) · 2.8 KB
/
AnnotationsTest.kt
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
package io.mockk.it
import io.mockk.MockKAnnotations
import io.mockk.MockKException
import io.mockk.every
import io.mockk.impl.annotations.*
import io.mockk.verify
import kotlin.test.*
class AnnotationsTest {
class MockCls {
fun op(a: Int): Int = a + 1
}
class AnnotatedCls {
@MockK
lateinit var mock: MockCls
@RelaxedMockK
lateinit var relaxedMock: MockCls
@SpyK
var spy = MockCls()
}
class ReadonlyAnnotatedCls {
@MockK
val mock: MockCls? = null
}
@Test
fun mock() {
val obj = AnnotatedCls()
MockKAnnotations.init(obj)
assertFailsWith(MockKException::class) {
obj.mock.op(1)
}
every { obj.mock.op(5) } returns 4
assertEquals(4, obj.mock.op(5))
verify { obj.mock.op(5) }
}
@Test
fun relaxedMock() {
val obj = AnnotatedCls()
MockKAnnotations.init(obj)
assertEquals(0, obj.relaxedMock.op(5))
verify { obj.relaxedMock.op(5) }
}
@Test
fun spy() {
val obj = AnnotatedCls()
MockKAnnotations.init(obj)
assertEquals(6, obj.spy.op(5))
verify { obj.spy.op(5) }
}
@Test
fun readonlyError() {
val obj = ReadonlyAnnotatedCls()
assertFailsWith(MockKException::class) {
MockKAnnotations.init(obj)
}
}
class InjectionTarget1(val obj: MockCls)
class InjectionTarget2 {
lateinit var obj: MockCls
}
class InjectionTarget3{
val obj = MockCls()
}
class InjectionTarget4{
lateinit var obj: MockCls
fun op(a: Int): Int {
return obj.op(a)
}
}
class InjectSourceCls {
@MockK
lateinit var mock: MockCls
@InjectMockKs
lateinit var target1: InjectionTarget1
@InjectMockKs
lateinit var target2: InjectionTarget2
@OverrideMockKs
lateinit var target3: InjectionTarget3
}
@Test
fun inject() {
val source = InjectSourceCls()
MockKAnnotations.init(source)
assertNotNull(source.mock)
assertSame(source.mock, source.target1.obj)
assertSame(source.mock, source.target2.obj)
assertSame(source.mock, source.target3.obj)
}
class InjectIntoSpySourceCls {
@MockK
lateinit var mock: MockCls
@InjectMockKs
@SpyK
lateinit var target4: InjectionTarget4
}
@Test
fun injectIntoSpy() {
val callValue = 5
val source = InjectIntoSpySourceCls()
MockKAnnotations.init(source)
every { source.mock.op(any()) } returns callValue
assertNotNull(source.target4)
assertEquals(callValue, source.target4.op(0))
verify { source.target4.op(any()) }
}
}