Questions tagged [mockk]

Mockk is a free, open source mocking framework for Kotlin programming language. With some features similar to Mockito and Powermock, Mockk enables Kotlin developers to mock Kotlin features with a simple DSL, allowing for simple and concise testing code.

Mockk is a Mocking library that enables developers to test their code with a simple DSL. With familiar features from and , Mockk enables developers to test their / code in a simple but powerful fashion.

val car = mockk<Car>()

every { car.drive(Direction.NORTH) } returns Outcome.OK

car.drive(Direction.NORTH) // returns OK

verify { car.drive(Direction.NORTH) }
521 questions
6
votes
3 answers

Kotest with Mockk: How to clear verify count

So I have the following code: When("SMS with location update command is received") { every { context.getString(R.string.location_sms, any(), any(), any(), any()) } returns "loc" …
SMGhost
  • 3,867
  • 6
  • 38
  • 68
6
votes
3 answers

Mock private property with mockk throws an excpetion

I'm using mockk for my testing in kotlin. But I can't seem to override a private property in a spy object. I have this object private val driverMapSnapshotMap: MutableMap = mutableMapOf() in a class that I spy on using viewModel…
orelzion
  • 2,452
  • 4
  • 30
  • 46
6
votes
1 answer

how to return two different mocks from same function depending on passed parameters in mockk?

I am testing one method. It requests the same function of a mocked object twice but with different parameters passed. Naturally, I need two different answers, but mockk gives me the same answer for both. every { userRepository.getUser("A") }.answers…
6
votes
2 answers

MockK - verify failing with arguments not matching

I've noticed that sometimes verify fails with "... call to ... happened, but arguments are not matching" Here is a sample test that shows verify failing: class TestStuff { val stuff = "1" @RelaxedMockK lateinit var testService:…
jc12
  • 1,411
  • 1
  • 18
  • 24
5
votes
0 answers

MockK : How to print all interactions of a mock

What is the equivalent of the below Mockito method in 'MockK' mockingDetails(mock).printInvocations()
pablogeorge
  • 233
  • 1
  • 10
5
votes
1 answer

mockkStatic and mockkObject doesn't mock companion objects in Android

I have a very simple class: class TestClass { companion object { fun sampleFunc(value: Int): Int { return value + 5 } } } and a very simple test: @Test fun `test class`() { mockkObject(TestClass::class) …
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
5
votes
3 answers

@MockK or mockk()

When using the Mockk for Android Unit tests, there is any difference of using the notation when declaring dependencies? class Test { private val x: X = mockk() private val test = TestClass(x) ... } or class Test { @MockK private…
Canato
  • 3,598
  • 5
  • 33
  • 57
5
votes
1 answer

MockK: return different result for different type parameter in generic function

I am trying to return different values for different type parameters in my generic function, but on some reason only one of the mocks is taken into account (the latest, as it seems). I have tried four different approaches, they all are listed…
5
votes
1 answer

Getting error MockKException: no answer found for: Observer(#8).onChanged Android

I'm writing a unit test. Below is my code. The architecture is MVVM using Dagger2. I'm calling the login function residing in the LoginViewModel, which is notifying the getLoginState function. The error I'm getting is: Error: …
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35
5
votes
1 answer

How to mock ObjectMapper.readValue() using mockk

There was a similar question asked about Mockito here I have a situation where I would like to mock out readValue in the following line val animal: Animal = objectMapper.readValue(String(message.body)) I tried @Test fun `test you filthy animal`()…
Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51
5
votes
1 answer

getting error Missing calls inside every { ... } block in writing unit test cases in kotlin + Mockk + Junit5

the function I am testing, class FileUtility { companion object { @JvmStatic fun deleteFile(filePath: String) { try { val file = getFileObject(filePath) file.delete() }…
Akash Patel
  • 189
  • 1
  • 5
  • 13
5
votes
1 answer

Mockk: How to mock a return after a delay?

I need to mock a call to some class and make it take some time. The current code uses this: every { useCase.execute(any()) } answers { AnswersWithDelay(50000, DoesNothing.doesNothing()) } Now I am changing execute() to return an object of…
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
5
votes
3 answers

Mock constant values in with mockk

We currently have an object that consists of constant values only. object Constants { const val VERSION = V1 } However, these constants' values may be altered in the future. Thus I want to ensure that a test breaks if certain conditions are…
Robin Ellerkmann
  • 2,083
  • 4
  • 29
  • 34
5
votes
1 answer

Mocking extensions from Continuation

I want to mock resume and resumeWithException from the standard library Continuation class. Both are extension functions. Here's my JUnit setup function: @MockK private lateinit var mockContinuation: Continuation @Before fun setup() { …
m0skit0
  • 25,268
  • 11
  • 79
  • 127
5
votes
2 answers

Test SharedPreferences in Android

I am coming into a legacy codebase which uses SharedPreferences for data persistence. I'd like to test saving/retrieving a value, mocking using MockK. However, the assertion in this unit test never passes. It is as if the SharedPrefs aren't…
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147