forked from mockk/mockk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerificationErrorsTest.kt
192 lines (153 loc) · 5.42 KB
/
VerificationErrorsTest.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
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
package io.mockk.it
import io.mockk.*
import kotlin.test.Test
import kotlin.test.fail
class VerificationErrorsTest {
val mock = mockk<MockCls>("mock")
val openMock = mockk<OpenMockCls>("mock")
private fun expectVerificationError(vararg messages: String, block: () -> Unit) {
try {
clearMocks(mock, openMock)
block()
fail("Block should throw verification failure")
} catch (ex: AssertionError) {
if (messages.any { !ex.message!!.contains(it) }) {
fail("Bad message: " + ex.message)
}
}
}
@Test
fun notMatchingArguments() {
expectVerificationError(
"Only one matching call to ",
"but arguments are not matching",
"MockCls.otherOp"
) {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
verify { mock.otherOp(1, 3) }
}
}
@Test
fun notMatchingArgumentsWithSameMethod() {
expectVerificationError("No matching calls found.", "Calls to same method", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
mock.otherOp(1, 4)
verify { mock.otherOp(1, 3) }
}
}
@Test
fun wasNotCalledToSameMock() {
expectVerificationError("was not called", "Calls to same mock", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
verify { mock.manyArgsOp(true, false) }
}
}
@Test
fun wasNotCalled() {
expectVerificationError("was not called") {
verify { mock.otherOp(1, 2) }
}
}
@Test
fun someMatchingCallsFoundButNotAll() {
expectVerificationError("2 matching calls found, but needs at least 3 calls", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
mock.otherOp(1, 2)
verify(atLeast = 3) { mock.otherOp(1, 2) }
}
}
@Test
fun oneMatchingCallFoundButNeedMore() {
expectVerificationError("One matching call found, but needs at least 3 calls", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
verify(atLeast = 3) { mock.otherOp(1, 2) }
}
}
@Test
fun callsAreNotInVerificationOrder() {
expectVerificationError("calls are not in verification order", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
mock.otherOp(1, 3)
verifyOrder {
mock.otherOp(1, 3)
mock.otherOp(1, 2)
}
}
}
@Test
fun lessCallsHappenedThanDemanded() {
expectVerificationError("fewer calls happened than demanded by order verification sequence", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 3)
verifyOrder {
mock.otherOp(1, 3)
mock.otherOp(1, 2)
}
}
}
@Test
fun notFullSequence() {
expectVerificationError(
"number of calls happened not matching exact number of verification sequence",
"MockCls.otherOp"
) {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 3)
verifySequence {
mock.otherOp(1, 3)
mock.otherOp(1, 2)
}
}
}
@Test
fun callsNotMatchinVerificationSequence() {
expectVerificationError("calls are not exactly matching verification sequence", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
mock.otherOp(1, 3)
verifySequence {
mock.otherOp(1, 3)
mock.otherOp(1, 2)
}
}
}
@Test
fun someCallsWereNotMatched() {
expectVerificationError("some calls were not matched", "MockCls.otherOp") {
every { mock.otherOp(1, any()) } answers { 2 + firstArg<Int>() }
mock.otherOp(1, 2)
mock.otherOp(1, 3)
verifyAll {
mock.otherOp(1, 2)
}
}
}
data class IntWrapper(val data: Int)
class MockCls {
fun otherOp(a: Int = 1, b: Int = 2): Int = a + b
fun manyArgsOp(
a: Boolean = true, b: Boolean = true,
c: Byte = 1, d: Byte = 2,
e: Short = 3, f: Short = 4,
g: Char = 5.toChar(), h: Char = 6.toChar(),
i: Int = 7, j: Int = 8,
k: Long = 9, l: Long = 10,
m: Float = 10.0f, n: Float = 11.0f,
o: Double = 12.0, p: Double = 13.0,
q: String = "14", r: String = "15",
s: IntWrapper = IntWrapper(16), t: IntWrapper = IntWrapper(17)
): Double {
return (if (a) 0 else -1) + (if (b) 0 else -2) + c + d + e + f + g.code.toByte() + h.code.toByte() +
i + j + k + l + m + n + o + p + q.toInt() + r.toInt() + s.data + t.data
}
}
abstract class OpenMockCls {
abstract fun op(a: Int = 1, b: Int = 2): Int
}
}