Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting 'Missing calls inside every { ... } block' when using mockk

I'm using MockK as mock library for kotlin, for some reason I get an error on the every{} line.

io.mockk.MockKException: Missing calls inside every { ... } block.

this is my test class

internal class TestClass {

  private val service: MyService = mockk(relaxed = true)
  private val dao: MyDao = mockk(relaxed = true)
  private val repository: MyRepository = MyRepositoryImp(
    service = service,
    dao = dao,
  )

  @Test
  fun test() = runBlocking {
    val expected = SOMETHING

    every { repository.getValues() } returns flowOf(SOMETHINGELSE) // crash

    assertEquals(1, 1)
  }
}

MyService is just a retrofit interface

internal interface MyService {

  @GET("123")
  suspend fun getRemoteData(): Response<SOMECLASS>

}

Anyone has any idea? thanks!

like image 894
TootsieRockNRoll Avatar asked Oct 31 '25 17:10

TootsieRockNRoll


1 Answers

You've instantiated the repository as a real instance. That means that it'll actually execute the code inside its implementation and that's known as the 'subject under test'.

But every { ... } returns ... needs a mockk instance.

So in your example, it should be

every { service.getRemoteData() } returns ... or every { dao.someMethod() } returns ...

like image 67
Chris Avatar answered Nov 02 '25 06:11

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!