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
18
votes
2 answers

MockK - reinitialize mocks for each test

I have some mocks created using: val someService = mockk(relaxed = true) There are multiple tests in the file and I want the mock to be reset for each test Is there currently a way to do this in MockK? I know there is…
jc12
  • 1,411
  • 1
  • 18
  • 24
17
votes
2 answers

Kotlin Mockk Error: Missing calls inside verify { ... } block

I already read some issues with this or a similar error message (it is also ocurring for every {}), but none of them took me to a success result. Any hints or suggestions on how to get this to work? Here is my setup and the Unit Test…
muetzenflo
  • 5,653
  • 4
  • 41
  • 82
16
votes
3 answers

Android UI tests with Espresso + MockK crash with SIGSEGV on emulators, fine on physical devices

I have just started using MockK to mock all the Repositories / Services logic in an MVP-based app for UI tests. I have some UI tests running a login activity where the Espresso inputs logins and passwords and using MockK I can fake various situation…
Mackovich
  • 3,319
  • 6
  • 35
  • 73
15
votes
6 answers

MockK - Failed matching mocking signature for left matchers: [any(), any()]

I want to implement some UI Tests to assure that the code implemented today works for tomorrow but when trying to see if already UI tests implemented in the past works, it throws this error: Caused by: io.mockk.MockKException: Failed matching…
Barrufet
  • 495
  • 1
  • 11
  • 33
15
votes
1 answer

Mock Locale using Mockk

I'm trying to use Mockk to mock the call to Locale.getDefault(), however I can't seem to get it working. Has anyone successfully used Mockk to mock the Locale? My very simple test class @Test fun testName() { val defaultLocale =…
Joao Sousa
  • 4,055
  • 1
  • 26
  • 29
14
votes
1 answer

mockk, what is just run

Could not find explanation about the "just run", what does it mean when stub a function with it? Will it make the mock object to call its real function, or make the function run a stub which does nothing? Is there sample for showing some real use…
lannyf
  • 9,865
  • 12
  • 70
  • 152
14
votes
1 answer

Kotlin Mockito always return object passed as an argument

I am trying to use Mockito on my mocked object in such a way that it should always return the very same object that was passed in as an argument. I tried it do to it like so: private val dal = mockk { …
Whizzil
  • 1,264
  • 6
  • 22
  • 39
13
votes
2 answers

How to test suspend function using MockK?

I am writing a unit test for my Datarepository layer which simply calls an interface. I am using Kotlin, coroutines and MockK for unit testing. In MockK, how can I verify that I have called apiServiceInterface.getDataFromApi() and has happened only…
BRDroid
  • 3,920
  • 8
  • 65
  • 143
13
votes
1 answer

mockk verify lambda was passed in mock

I'm trying to test the method getSongsList class SongsRemoteDataSource @Inject constructor( private val resultParser: ResultParser, private val songsService: SongsService ) { suspend fun getSongsList(query: String):…
dsantamaria
  • 188
  • 1
  • 8
13
votes
1 answer

mockk verify lambda argument

I'd like to verify the value which was passed in via a lamdba. The func looks like this: fun save(entity: Any, idSupplier: () -> UUID): JsonEntity { return save(JsonEntity(idSupplier(), entity, entity::class.simpleName!!)) } Now within my test…
tomhier
  • 570
  • 1
  • 5
  • 15
12
votes
1 answer

io.mockk.MockKException: no answer found for: SavedStateHandle(#1).set(Key, Something)

I have a ViewModel class as below (simplified to demonstrate the problem I faced) class MyViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() { init { savedStateHandle.set(KEY, "Something") } } I have a MockK…
Elye
  • 53,639
  • 54
  • 212
  • 474
12
votes
1 answer

mockk exception - no answer found for

using mockk for testing kotlin function. private val serviceObject = mockk() private val serviceToBeTested = ServiceToBeTestd(Service) fun test(){ when(serviceObject.function1(argument1,argument1)) …
Parameswar
  • 1,951
  • 9
  • 34
  • 57
12
votes
2 answers

Can we mock constructor and check its parameters?

With mockk, to mock constructors, we can do something like (taken from documentation): class MockCls { fun add(a: Int, b: Int) = a + b } mockkConstructor(MockCls::class) every { anyConstructed().add(1, 2) } returns 4 assertEquals(4,…
Happy
  • 1,815
  • 2
  • 18
  • 33
11
votes
2 answers

What does relaxed = true do in mockK?

I read document, but I don't still get it. The differences between this private val myClass: MyClass = mockk(relaxed = true) and this. private val myClass: MyClass = mockk() What I understood is if relaxed is true. Then, all the member fields or…
c-an
  • 3,543
  • 5
  • 35
  • 82
10
votes
2 answers

mockk every {}.throws() Exception fails test

I need to verify that a certain call is not made, when a previous method call throws an Exception. // GIVEN every { relaxedMock.eats() }.throws(NotHungryException()) // WHEN sut.live() // THEN verify (exactly = 0) {…
yN.
  • 1,847
  • 5
  • 30
  • 47
1
2
3
34 35