Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #785 from npars/mock-invisible-parent
Support mocking non-accessible parent methods
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
mockk/jvm/src/test/kotlin/io/mockk/it/PrivateParentMethodTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"]() } | ||
} | ||
} |