question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong value when mocking kotlin.Result

See original GitHub issue

Prerequisites

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:closed
  • Created a year ago
  • Reactions:9
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
pphilippcommented, Oct 31, 2022

Seems like the issue comes back. I have the Result.failure(Exception()) which wrapped with Result.success(Result.failure(Exception())).

version io.mockk:mockk:1.13.2

3reactions
qoomoncommented, Aug 23, 2022

It was fixed with recent version 1.12.6

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found