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
66
votes
10 answers

Unit testing coroutines runBlockingTest: This job has not completed yet

Please find below a function using a coroutine to replace callback : override suspend fun signUp(authentication: Authentication): AuthenticationError { return suspendCancellableCoroutine { …
suns9
  • 875
  • 2
  • 8
  • 17
56
votes
7 answers

Mock static java methods using Mockk

We are currently working with java with kotlin project, slowly migrating the whole code to the latter. Is it possible to mock static methods like Uri.parse() using Mockk? How would the sample code look like?
Andrzej Sawoniewicz
  • 1,541
  • 2
  • 16
  • 18
50
votes
9 answers

Mockk Missing calls inside every { ... } block

I'm stuck trying to mock some stuff with mockk: I have the following setup on gradle root: |-- App (just a sample app for the SDK) |-- SDK (SDK we develop) << apply plugin: 'com.android.library' |-- SDKimpl.kt |-- Foo (wrapper around a…
Budius
  • 39,391
  • 16
  • 102
  • 144
47
votes
4 answers

mocking only one call at a time with mockk

I know that in order to mock how a method responds, you have to use every { instanceX.methodB() } returns "42" I'm trying to mock an iterator, for which you have to mock 2 methods hasNext() and next(), if hasNext() returns true always there will…
BadChanneler
  • 1,450
  • 1
  • 12
  • 20
46
votes
7 answers

MockK "io.mockk.MockKException: no answer found for:" error

Hi i am trying to mock the response i get from a Single observable that gets returned from retrofit using a delegator that my presenter class calls and i am getting the following error: io.mockk.MockKException: no answer found for:…
Jono
  • 17,341
  • 48
  • 135
  • 217
40
votes
5 answers

How do you Mockk a Kotlin top level function?

Mockk allows mocking static functions, but how does one mock a Kotlin top level function? For example, if I have a Kotlin file called HelloWorld.kt, how do I mock the sayHello() function? HelloWorld.kt fun sayHello() = "Hello Kotlin!"
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
36
votes
3 answers

How to call a lambda callback with mockk

I create a mock of a class with mockk. On this mock I now call a method that gets a lambda as a parameter. This lambda serves as a callback to deliver state changes of the callback to the caller of the method. class ObjectToMock() { fun…
Janusz
  • 187,060
  • 113
  • 301
  • 369
22
votes
1 answer

How to mock android context using mockk library

I am new in JUnit testing on Android and I'm testing a function, which is using android context object to get a string resources and making some comparsions. How can I mock android context object to successfully test this function? For testing I'm…
Taras Stavnychyi
  • 895
  • 1
  • 9
  • 14
22
votes
3 answers

How to mock Build.VERSION.SDK_INT using mockk

How can I mock Build.VERSION.SDK_INT in mockk? I've done the following: @Test fun testFoo(){ mockkStatic(Build::class) mockkStatic(Build.VERSION::class) every { Build.VERSION.SDK_INT } answers { 22 } } I end up getting…
JHowzer
  • 3,684
  • 4
  • 30
  • 36
21
votes
1 answer

How to check if a method was not invoked with mockk?

I need to check if a method was not invoked in my unit tests. This is an example test I did that checks if the method was invoked and it works perfectly fine: @Test fun viewModel_selectDifferentFilter_dispatchRefreshAction() { val selectedFilter…
Pierre Vieira
  • 2,252
  • 4
  • 21
  • 41
21
votes
4 answers

Mock a private property

Lets say we have a class like this: class Whatever { private var something = false fun aMethod(): Int { return if( something ) { 1 } else { 0 } } } According to the documentation, it…
Maalevolent
  • 472
  • 1
  • 5
  • 12
20
votes
1 answer

What's the difference between `verifySequence` and `verifyOrder` in MockK?

In the guide for MockK library, the example is not clearing this for me. Here follows the example from the documentation: class MockedClass { fun sum(a: Int, b: Int) = a + b } val obj = mockk() val slot = slot() every { …
Sevastyan Savanyuk
  • 5,797
  • 4
  • 22
  • 34
20
votes
2 answers

Kotlin Unit testing - How to mock component of Companion object?

If I have a (simplified) class that looks like this: class MyManager @JvmOverloads constructor(/*constructor args*/) : MyManagerInterface { @Inject lateinit var myLogger: MyLogger init { component =…
the_new_mr
  • 3,476
  • 5
  • 42
  • 57
18
votes
5 answers

Can you verify a property setter using mockk?

We had a few tests in Java and Mockito, which we are progressively converting into Kotlin and Mockk. There's a problem, though. This simple line: verify(mockedInteractor).setIndex(1); When we move it to mockk, we get this: verify {…
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
18
votes
1 answer

How to capture an argument that was passed to a mocked function and return it?

So in the service I am testing, I have a depending service which is taking an object and does some augmenting on it. I want to mock the part the depending service is doing and make the mock return exactly what it's receiving. Problem is I don't have…
BadChanneler
  • 1,450
  • 1
  • 12
  • 20
1
2 3
34 35