Comments (15)
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 …?
from mockk.
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))) }
}
from mockk.
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 abstract class
"implementing" the interface I want to test, but instead of mockk
-ing it, I use spyk
call on the abstract class
, that way i do not need to declare the callOriginal()
on methods I want to actually test.
from mockk.
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?
from mockk.
It should, but the fix hasn't been released yet
from mockk.
Sorry about the delay in the release, planning to make one next week.
from mockk.
This is blocking some of my tests as well...
from mockk.
Same here
from mockk.
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 stale
just put an important
tag.
from mockk.
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?
from mockk.
Same issue here. Any feedbacks?
from mockk.
I'm also blocked writing a test like this because of this problem. Any plans to fix it?
from mockk.
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))) }
}
from mockk.
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.
from mockk.
@flapenna : Is this issue fixed by above PR?
from mockk.
Related Issues (20)
- Allow to copy an existing mock HOT 1
- lateinit property has not been initialized on some ancestor classes HOT 2
- Using the result of callOriginal() inside coAnswers throws a fatal exception when the original contains a call to delay
- KotlinReflectionInternalError with HttpResponse.BodyHandler HOT 4
- Cannot create mock of interface, but of implementing class (method type parameters involved) HOT 2
- Error msg missing type information when no answers found for an overloaded method HOT 2
- kotlinx.datetime.Instant relaxed mock yields null java.time.Instant value
- NoSuchElementException in verify() HOT 3
- Bug: verify on anyConstructed<Mock>() throw an exception
- mockk-1.13.10 breaks tests that mock returning a value class HOT 2
- UInt properties cannot be mocked
- Poor performance with lateinit mockks
- Value class unboxing fails when mocked function and caller have the same name HOT 1
- mockkStatic/mockkObject not working HOT 2
- mockkObject not working in Maven
- Mockk deadlock during parallel tests
- Mocking doesn't work for a functional interface returning a value class
- Any.get() extension function is colliding with other classes extension function
- Can`t mock private property (Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock) HOT 1
- Cannot mock kotlin object's get property that returns value class
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockk.