Skip to content
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

kotlin.Result<T> mocking is not working as expected #645

Open
3 tasks done
siboxd opened this issue Jun 18, 2021 · 14 comments
Open
3 tasks done

kotlin.Result<T> mocking is not working as expected #645

siboxd opened this issue Jun 18, 2021 · 14 comments

Comments

@siboxd
Copy link

siboxd commented Jun 18, 2021

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version (checked on 1.10.6 and 1.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 ] -----------------------
@pawelsmagala
Copy link

This is known problem, it should be solved by this #633
Now we just need to wait for release.

@stale
Copy link

stale bot commented Aug 21, 2021

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 ask to put an important label.

@stale stale bot added the stale label Aug 21, 2021
@siboxd
Copy link
Author

siboxd commented Aug 24, 2021

I've seen the new version 1.12.0 of Mockk and I gave it a try.

For some reason the assertSame still gives an assertion error...

expected same:<Success(com.my.test.MyTest$Data@7dd22fe3)> was not:<Success(com.my.test.MyTest$Data@7dd22fe3)>
Expected :Success(com.my.test.MyTest$Data@7dd22fe3)
Actual   :Success(com.my.test.MyTest$Data@7dd22fe3)
<Click to see difference>

Have you tried my use case with the new version 1.12.0?

@stale stale bot removed the stale label Aug 24, 2021
@b22n
Copy link

b22n commented Sep 29, 2021

It is reproduced in 1.12.0 (kotlin 1.5.21 ~ 1.5.31)

@nuhkoca
Copy link

nuhkoca commented Sep 30, 2021

This is still occurring in 1.12.0

@milken
Copy link

milken commented Jan 20, 2022

I can confirm it still happens for value class in 1.12.2.
I'm staying on 1.10.2 until resolved :(

@stale
Copy link

stale bot commented Apr 16, 2022

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 ask to put an important label.

@stale stale bot added the stale label Apr 16, 2022
@siboxd
Copy link
Author

siboxd commented Apr 16, 2022

Any news on this? Could you put the important label?

@stale stale bot removed the stale label Apr 16, 2022
@stale
Copy link

stale bot commented Jul 10, 2022

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 ask to put an important label.

@stale stale bot added the stale label Jul 10, 2022
@siboxd
Copy link
Author

siboxd commented Jul 11, 2022

The issue is still present with: kotlin 1.6.21 and mockk 1.12.4

The assertSame still gives an assertion error...

Any news on this issue? Could you put the important label?

@stale stale bot removed the stale label Jul 11, 2022
@stale
Copy link

stale bot commented Nov 2, 2022

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 ask to put an important label.

@stale stale bot added the stale label Nov 2, 2022
@siboxd
Copy link
Author

siboxd commented Nov 2, 2022

Any news on this? Could you put the important label?

@stale stale bot removed the stale label Nov 2, 2022
@behnawwm
Copy link

It's still happening in 1.13.8 and Kotlin 1.9

@aditya-dodda
Copy link

Same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants