New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: callOriginal() for default interface methods is not working #64
Comments
This is blocking some of my tests as well... |
Same here |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as |
What's the status on this one? I tried using this when mocking an interface like so class InterfaceMock : Interface by mockk() {
init {
coEvery { defaultSuspendFunctionCall(any()) } coAnswers { callOriginal() }
}
} but it does not seem to work. Am I using it incorrectly or is it still not fixed? |
This is a HUGE blocker, making testing default functions if not impossible then at least horribly complex instead of straight-forward. It was reported over 3 years ago and not even a feedback available here. What the …? |
Same issue here. Any feedbacks? |
I'm also blocked writing a test like this because of this problem. Any plans to fix it? |
Hello everyone! I was encountered with this too and I wish to fix it :) So, I can start to fix it but are there any clues or advice about it? |
I've banged my head against this for a while and found an workaround. Annoying but workable in the interim: import io.mockk.*
data class ItemData(val id: Int)
interface Database {
fun insertItems(items: List<ItemData>)
fun deleteItems(items: List<ItemData>)
fun runTransaction(alsoDelete: Boolean) {
insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3)))
if (alsoDelete)
deleteItems(listOf(ItemData(id = 4), ItemData(id = 5), ItemData(id = 6)))
}
}
fun main(args: Array<String>) {
// val x = mockk<Database>() // this should work, but doesn't due to https://github.com/mockk/mockk/issues/64
// instead we create an anonymous object that implements the interface (but doesn't actually implement it; we stub the methods)
val x = spyk(object: Database {
override fun deleteItems(items: List<ItemData>) {}
override fun insertItems(items: List<ItemData>) {}
})
// the members/methods of the interface can be stubbed if needed:
// every { x.insertItems(any()) } just Runs
// every { x.deleteItems(any()) } just Runs
every { x.runTransaction(any()) } answers { callOriginal() }
x.runTransaction(alsoDelete = true)
verify { x.insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3))) }
}
|
quick update, a much cleaner option works with abstract classes: import io.mockk.*
data class ItemData(val id: Int)
interface Database {
fun insertItems(items: List<ItemData>)
fun deleteItems(items: List<ItemData>)
fun runTransaction(alsoDelete: Boolean) {
insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3)))
if (alsoDelete)
deleteItems(listOf(ItemData(id = 4), ItemData(id = 5), ItemData(id = 6)))
}
}
fun main(args: Array<String>) {
// val x = mockk<Database>() // this should work, but doesn't due to https://github.com/mockk/mockk/issues/64
// instead we create an abstract class to work around the issue.
abstract class DatabaseMock: Database
val x = mockk<DatabaseMock>()
// the members/methods of the interface need to be stubbed too:
every { x.insertItems(any()) } just Runs
every { x.deleteItems(any()) } just Runs
every { x.runTransaction(any()) } answers { callOriginal() }
x.runTransaction(alsoDelete = true)
verify { x.insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3))) }
}
|
This is a problem caused by Mockk not being able to reliably spy interfaces. The solution I am using is also by creating an in-place |
Ran into this as well today. I resolved with @zakhenry's workaround, but it'd be great if this could be fixed. Makes tests a bit ugly and confusing for others when they see this empty abstract class sitting there. |
Sorry about the delay in the release, planning to make one next week. |
Should work
The text was updated successfully, but these errors were encountered: