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.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:12

github_iconTop GitHub Comments

3reactions
siboxdcommented, Apr 16, 2022

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

1reaction
deadeyyecommented, Jun 21, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange behavior with Kotlin.Result : KT-30223 - YouTrack
This mismatch can have different symptoms, e.g. a mocking framework throwing an exception when one tries to declare a return of a boxed...
Read more >
Problems with Kotlin Result<T> on unit tests - Stack Overflow
This issue appears to happens specifically when using Mockito's .thenReturn on suspend functions. I've found that using .thenAnswer doesn't ...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
In this article, we'll cover creating and configuring mocks and using them to verify the expected behavior of the system being tested. We'll...
Read more >
Build local unit tests - Android Developers
However, not being able to interact with the Android framework creates a ... whether the components in your app return the expected results....
Read more >
Verify that functions were called | Mocking | MockK Guidebook
Inside the verification block (between the opening curly bracket { and closing curly bracket } ), you write the method you want 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