forked from mockk/mockk
/
PrivateParentMethodTest.kt
66 lines (52 loc) · 1.56 KB
/
PrivateParentMethodTest.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
package io.mockk.it
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlin.test.Test
import kotlin.test.assertEquals
/**
* Test mocking non-visible parent methods through dynamic calls. Issue #425
*/
class PrivateParentMethodTest {
open class Parent {
open fun call(): String = callPrivate()
private fun callPrivate() = "Real"
}
open class Child: Parent()
class ChildWithShadowedMethod: Parent() {
override fun call(): String = callPrivate()
fun callPrivate() = "Shadowed"
}
class GrandChild: Child()
@Test
fun testChildAlwaysMockedFirst() {
val mock = mockk<ChildWithShadowedMethod> {
every { call() } answers { callOriginal() }
every { this@mockk["callPrivate"]() } returns "Mock"
}
assertEquals(mock.call(), "Mock")
}
@Test
fun testPrivateCallMock() {
val mock = mockk<Child> {
every { call() } answers { callOriginal() }
every { this@mockk["callPrivate"]() } returns "Mock"
}
assertEquals(mock.call(), "Mock")
}
@Test
fun testPrivateCallMockForGrandChild() {
val mock = mockk<GrandChild> {
every { call() } answers { callOriginal() }
every { this@mockk["callPrivate"]() } returns "Mock"
}
assertEquals(mock.call(), "Mock")
}
@Test
fun testPrivateCallVerify() {
val mock = spyk(Child(), recordPrivateCalls = true)
mock.call()
verify { mock["callPrivate"]() }
}
}