Wrong value when mocking kotlin.Result
See original GitHub issuePrerequisites
Please answer the following questions for yourself before submitting an issue.
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
Failure Information (for bugs)
In mockk 1.12.4 I was able to mock a kotlin.Response
return value on a function and get the generic value from it.
1.12.5 seems to have broken this and regardless of the Response.success(x)
value, success always folds to a boolean false
.
Code below.
Context
- MockK version: 1.12.5
- OS: macOS 11.5.2
- Kotlin version: 1.7.10
- JDK version: 11
- JUnit version: 4.13.2
- Type of test: unit test
Minimal reproducible code (the gist of this issue)
// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
testImplementation("org.hamcrest:hamcrest-library:2.2")
testImplementation("junit:junit:4.13.2")
testImplementation("io.mockk:mockk:1.12.5")
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
import io.mockk.every
import io.mockk.mockk
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
class ResultTest {
interface ToMock {
fun returnsResult(): kotlin.Result<String>
}
@Test
fun `result value`() {
val value = Result.success("something")
val toMock = mockk<ToMock> {
every { returnsResult() } returns value
}
// Expected: <Success(something)>
// but: was <Success(false)>
assertThat(toMock.returnsResult(), equalTo(value))
}
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
Issue Analytics
- State:
- Created a year ago
- Reactions:9
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Problems with Kotlin Result<T> on unit tests - Stack Overflow
The problem is, somehow the value of the Result object is wrapped by another Result , and we can pull the desired value...
Read more >Strange behavior with Kotlin.Result : KT-30223
This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong. User Agreement Review now...
Read more >A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Mockito doesn't care what exact value is being passed to when() because it stores information about a mocked method's invocation in so-called 'ongoing...
Read more >Mocking is not rocket science: MockK advanced features
MockK is skyrocketing in Kotlin world during the last year. ... This result in a nice DSL to define dependent objects:
Read more >Kotlin Unit Testing with Mockk - Marco Cattaneo - Medium
Mockk is a Kotlin Library for mocking inside the Unit Test. ... if the method returns a value or use a callback to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seems like the issue comes back. I have the
Result.failure(Exception())
which wrapped withResult.success(Result.failure(Exception()))
.version io.mockk:mockk:1.13.2
It was fixed with recent version
1.12.6