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.

How to mock a suspend function on an existing mock object?

See original GitHub issue

I have mock A built and given to me. Now, I need to mock a suspend API to return a value foo.

Example:

@Inject lateinit var mockA: A // using dagger to provide mocks configured in a separate module.

@Test
fun test1() = runBlocking {
    // how do i mock a suspend method on mockA?
}

_Originally posted by @andrewborba10 in https://github.com/nhaarman/mockito-kotlin/issues/269#issuecomment-429467218_

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

48reactions
Hurricaaanecommented, Jan 14, 2019

With mockito-kotlin:2.1.0 :

@Test
fun getFoo_validObject_returnsBarData() = runBlocking {
    aRepoMock.stub {
        onBlocking { getFoo() }.doReturn(BarData(42))
    }
   
    val result = classUnderTest.methodThatCallsGetFoo()
    assertTrue(result is BarData)
}

I’m not entirely sure though.

27reactions
stawil15commented, Nov 27, 2018

I know this comment will probably be considered unhelpful, but I recently spent a lot of time trying to do the same thing and I eventually came to the conclusion that, assuming no actual business logic is is taking place in your async block/function, I don’t really get anything out of including it in my tests. Testing that a mocked class returns the mocked data that I mocked for it isn’t actually useful.

That is to say, my suspend functions are only there to retrieve data (network calls) and I should focus on testing the function that contains the business logic for once those calls are returned.

Granted, if there is a way to do what you’re asking for it can still be very useful and I want to know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In kotlin, how do I mock a suspend function that wraps a ...
Mock your object, lets name it myObject (myObject has suspend method named isFoo). then: myObject.stub { onBlocking { isFoo() }.doReturn(true) }.
Read more >
Coroutines and suspend functions | Mocking | MockK Guidebook
Using coEvery, coVerify, and more to mock coroutines. ... MockK uses function literals to create stubs, small changes are needed to stub suspend...
Read more >
Mocking Coroutines - ProAndroidDev
As you can see testing functionality, that is built with Coroutines adds a bit of complexity: suspend functions can't just be called from...
Read more >
Testing Kotlin coroutines on Android
This guide covers how suspending functions can be tested, the testing constructs you need to be familiar with, and how to make your...
Read more >
MockK | mocking library for Kotlin
Provides DSL to mock behavior. Built from zero to fit Kotlin language. Supports named parameters, object mocks, coroutines and extension function mocking.
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