-
-
Notifications
You must be signed in to change notification settings - Fork 332
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: Can't mock kotlinx.coroutines.channels.Channel #231
Comments
Probably related: spying on a channel is impossible (?) too: package com.example
import io.mockk.*
import kotlinx.coroutines.channels.Channel
import org.junit.Test
class ChannelMockFailure {
@Test
fun `spying on a channel is impossible?`() {
val spy = spyk(Channel<Any?>(), name = "channel spy")
every { spy.cancel() } just runs
spy.cancel()
verify { spy.cancel() }
}
} |
The spy fails with the following stacktrace:
|
Wait..... the results are not deterministic. On some runs the aforementioned test methods pass, either one of them or both! |
Something weird is happening. |
I have no time to check it today |
Thanks for checking it out. I can't help you with that, so I hope you can reproduce whatever ByteBuddy magic is going on here! And then fix it or file an issue there. Good luck. |
Added ByteBuddy issue: raphw/byte-buddy#596 |
Added KT ticket: https://youtrack.jetbrains.com/issue/KT-29658 |
One more thing: Kotlin/kotlinx.coroutines#956 |
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 |
I've re-run the following test code many times (over 100 runs) and with MockK v1.9.3, Kotlin v1.3.50, Kotlin coroutines v1.3.2, JUnit v4.12, Android {target=28, min=16, compile=28}, it passed every time. package com.example
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.spyk
import io.mockk.unmockkAll
import io.mockk.verify
import kotlinx.coroutines.channels.Channel
import org.junit.After
import org.junit.AfterClass
import org.junit.Test
class ChannelMockFailure {
companion object {
@JvmStatic
@AfterClass
fun `after class`() = unmockkAll()
}
@After
fun after() = clearAllMocks()
@Test
fun `mocking a channel is possible!`() {
mockk<Channel<Any?>>(name = "channel mock") { verifyChannelCancellation() }
}
@Test
fun `spying on a channel is possible!`() {
spyk(Channel<Any?>(), name = "channel spy") { verifyChannelCancellation() }
}
private fun Channel<Any?>.verifyChannelCancellation() {
every { cancel() } just runs
cancel()
verify { cancel() }
}
} I think this issue can be closed. Can you verify it works on your machine before closing, @oleksiyp? |
The issue was fixed on kotlinx.coroutines side |
Nvm, I found it: Kotlin/kotlinx.coroutines@27b8f45 It's in v1.3.5. |
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
I expect to be able to mock
Channel
fromkotlinx.coroutines.channels
.Current Behavior
I get a
java.lang.AbstractMethodError
.Failure Information (for bugs)
It seems that
Channel
cannot be mocked.Steps to Reproduce
Run the code below. Fail.
Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
OR android instrumented testFailure Logs
N/A
Stack trace
Minimal reproducible code (the gist of this issue)
The text was updated successfully, but these errors were encountered: