kotlin.Result<T> mocking is not working as expected
See original GitHub issuePrerequisites
Please answer the following questions for yourself before submitting an issue.
- I am running the latest version (checked on
1.10.6
and1.11.0
) - I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
Expected Behavior
I expect that mockk treats kotlin.Result<T>
class as other classes
Current Behavior
It seems that it is unnecessary wrapping any Result<T>
return type to another Result.success
instance making even a basic test fail.
Failure Information (for bugs)
I have no useful information at this time… I’ll add if I find useful info.
Steps to Reproduce
Let’s consider this piece of code:
class Data
interface MyInterface {
fun getData(): Result<Data>
}
Now I’m gonna test, with a mockk of MyInterface
, that it returns the instance of Data
class wrapped in a Result.success
instance:
@Test
fun testWithBug() {
val myDataResult = Result.success(Data())
val myInterface = mockk<MyInterface> {
every { getData() } returns myDataResult
}
assertSame(myDataResult, myInterface.getData())
}
This test fails with:
java.lang.AssertionError: expected same:<Success(com.my.test.MyTest$Data@6e0cff20)> was not:<Success(Success(com.my.test.MyTest$Data@6e0cff20))>
Expected :Success(com.my.test.MyTest$Data@6e0cff20)
Actual :Success(Success(com.my.test.MyTest$Data@6e0cff20))
But there’s no code wrapping the Data()
in two successes… from where does it come?
The thing is that it’s a problem of the Result
class because if you do this, the test passes:
class Data
interface MyInterface {
fun getData(): Data
}
@Test
fun testWithBug() {
val myDataResult = Data()
val myInterface = mockk<MyInterface> {
every { getData() } returns myDataResult
}
assertSame(myDataResult, myInterface.getData())
}
Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
- MockK version: 1.10.6 and 1.11.0
- OS: macOS 10.15.7
- Kotlin version: 1.5.10
- JDK version: 1.8.0_282
- JUnit version: 4.13.2
- Type of test: unit test
Minimal reproducible code (the gist of this issue)
// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
testImplementation "junit:junit:4.13.2"
testImplementation "io.mockk:mockk:1.11.0"
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
package com.my.test
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertSame
import org.junit.Test
class MyTest {
class Data
interface MyInterface {
fun getData(): Result<Data>
}
@Test
fun testWithBug() {
val myDataResult = Result.success(Data())
val myInterface = mockk<MyInterface> {
every { getData() } returns myDataResult
}
assertSame(myDataResult, myInterface.getData())
}
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:12
Top GitHub Comments
Any news on this? Could you put the important label?
This is known problem, it should be solved by this https://github.com/mockk/mockk/pull/633 Now we just need to wait for release.