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
4
votes
1 answer

io.mockk.MockKException: no answer found when mocking list of objects with ObjectMapper.readValue() using mockk?

There were similar questions but none dealing specifically with kotlin, mockk and using objectMapper.readValue to read a list of objects. Given a method: fun someMethod(message: Message): List = objectMapper.readValue( …
Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51
4
votes
1 answer

Stubbing a method on a spyk object calls the original method immediately

for example the method class SomeClass() { fun login(listener: (Boolean) -> Unit ) { do some async logic and then call the listener with the result } } val spy = spyk(SomeClass()) { val completion =…
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
4
votes
1 answer

What's the difference between mockk() and mockkClass()?

When I started learning Mockk for testing, I had the following question. Mockk official sample shows like: val car = mockk() and val car = mockkClass(Car::class) I seem the two as the same. What's the difference?
user3752013
  • 189
  • 1
  • 8
4
votes
1 answer

When using MockK coVerify, method was not called

The following test is failing. I have manually tested it, and it works. But I am not sure why the method is not called. I suppose it has to do with the fact I am testing a method in a coroutine, however, I am using a TestRule that sets the…
Jose Garcia
  • 148
  • 2
  • 13
4
votes
0 answers

Using Mockk to mock private function that takes a lambda

I am trying to write a unit test for a implementation of an abstract class I wrote. The method I'm trying to mock takes a lambda as it's only parameter. I'm trying to capture this lambda, so I can invoke it and get the result. This is the method…
jordond
  • 367
  • 4
  • 15
4
votes
0 answers

How to wait until system under test's internal Observer has executed its lambda

I'm testing MyFragment using MockK. The fragment registers an Observer for a public, mutable LiveData in the onViewCreated method. The Observer calls a method within a mocked ViewModel. However (based on reading log output, in the unit test, verify…
user1713450
  • 1,307
  • 7
  • 18
4
votes
1 answer

how to mock android.util.Log

I.m using a mockk library in kotlin, and in tests, I have the following exception: java.lang.RuntimeException: Method getStackTraceString in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details. I can't find a solution…
user12074594
4
votes
1 answer

How to mock objects in relaxed mode?

I have an object object Foo { fun doSomething(param: String) { throw Exception() } } I want it to become a stub (relaxed mock in mockk terminology) in my test. Other words, I want this test to pass without exception: @Test fun…
vdshb
  • 1,949
  • 2
  • 28
  • 40
4
votes
1 answer

Equivalent of doReturn(x).when(y)... in mockk?

I am looking for a mockk equivalent of doReturn(...).when(...).* I am working on writing some unit tests (testing contracts) that involves a lot of system classes and so need to intercept the methods that I don't control and return some call backs…
praveen_85
  • 182
  • 2
  • 13
4
votes
1 answer

How to mock String extension?

I'd like to mock a string extension. I've read the instructions how this should be done, by using mockStatic("kotlin.String") or mockkStatic("kotlin.kotlin_builtins") but it keeps saying Caused by: io.mockk.MockKException: Can't instantiate proxy…
tomhier
  • 570
  • 1
  • 5
  • 15
4
votes
2 answers

How to mock a private function in android test with MockK?

I can't seem to mock private functions in android tests. I'm also using the all-open plugin for pre-P testing. On non-android tests it runs with no problems. I figured it should work on android too, because it's marked on MockK-android. Is this not…
zabson
  • 614
  • 1
  • 10
  • 18
4
votes
1 answer

Can I mock a generic (template) private method using MockK in Kotlin?

I would like to mock the following function: private fun updateItemInDb(id: Long, column: String, data: T) which is invoked in the following way by my class: updateItemInDb(it, DB_POS, i), where it is a Long, DB_POS is String and i is an Int. I…
AlexSee
  • 322
  • 3
  • 16
3
votes
1 answer

KOTLIN and MOCKK UUID test

This is a Kotlin-Mockk based testcase, where I am trying to get the static class "UUID" to be mocked. this works when used to get random string but not UUID mockkStatic(UUID::class) every { UUID.randomUUID().toString() } returnsMany…
3
votes
1 answer

Mockk verify fails when checking called and wasNot called

I am trying to verify that a function was not called using the following: verify { managementService.deleteUser(any()) wasNot Called } That verification fails with the message: Verification failed: call 1 of…
HotN
  • 4,216
  • 3
  • 40
  • 51
3
votes
1 answer

How do I mock a Kotlin function type returning a value class with Mockk?

I have a test subject that takes a function type in its constructor: class PricedStockListLoader( val stock: Stock, val pricing: (Item) -> Price? ) In my test I can mock both dependencies: val stock: Stock = mockk() val pricing: (Item) ->…
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118