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

Mockk Mocking Private Properties in Kotlin

I have a simple class with a private field. class EmployeeData { private var employeeAge: Int = 0 fun getAge(): Int { return 1 + employeeAge } } I am trying to test this private employeeAge with the following from official…
user3949888
  • 137
  • 1
  • 1
  • 12
10
votes
3 answers

How to test a presenter MVP Android

I'm trying to understand how should I test my app, I'm still learning with mockito I also saw mockk but couldn't make it work, this is my Presenter class MyPresenterImpl @Inject constructor(var myUseCase: MyUseCase) : MyContract.Presenter { …
StuartDTO
  • 783
  • 7
  • 26
  • 72
10
votes
2 answers

How to mock a new object using mockk

I'm trying to write unit tests using mockk. I'm trying to figure out how to mock a new instance of an object. For example, using PowerMockito we would write: PowerMockito.whenNew(Dog::class.java).withArguments("beagle").thenReturn(mockDog) If the…
Scott
  • 1,263
  • 1
  • 12
  • 23
9
votes
2 answers

Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize

Hey I am getting this kind of weird issue. I don't understand why this is causing in my unit test. Can someone please guide me what is missing in my side. Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main…
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
9
votes
0 answers

Why is Robolectric 4.4 breaking my MockK tests

My current Android Application employs MockK in its junits All my tests runs as expected and with testImplementation 'org.robolectric:robolectric:4.3' However when I increase the version to testImplementation 'org.robolectric:robolectric:4.4' all my…
Hector
  • 4,016
  • 21
  • 112
  • 211
9
votes
2 answers

How to run lambda function passed to a mockked method?

I wonder whather it's possible to run a lambda function passed as a parameter to a mocked function. And run it whenever the mocked method is called. I am using Mockk and I imagine the code to be something like this: class DataManager { fun…
9
votes
2 answers

Android Kotlin Unit test failing with io.mockk.MockKException: no answer found for

EDIT: For future readers. I don't know if this question will help you very much.The logic of the fun has changed drastically so I am closing the question, but won't delete it. I am trying to write some unit tests for my ViewModel. I am using Mockk…
Sir Isaac
  • 107
  • 1
  • 2
  • 5
9
votes
1 answer

Match argument object that has a property equal to (using Mockk)

I have looked around for a similar question, but cannot find a solution. I have a couple of instances to the same type of object. As a simple example, a Pen object. This class contains size (Int) and color (String) properties. I need to mock a…
GiH
  • 365
  • 4
  • 16
9
votes
2 answers

Using mockk to match any varargs

I'm trying to mock an Android Context to return a string from a resource id. However I have trouble matching the stub to the call, I assume it is because of the varargs. However I am new to mockk so I might just miss something very easy. I mock the…
findusl
  • 2,454
  • 8
  • 32
  • 51
9
votes
2 answers

java.lang.IllegalStateException: Could not find sun.misc.Unsafe while @MockK

I'm having an issue when creating a mock using MockK. I have tried with kotlin 1.2.* version with MockK without Kotlin 1.3 but it also didn't help. How can I resolve this? Is this because of OpenJDK 11 of it's something else? Java version: openjdk…
lukaszrys
  • 1,656
  • 4
  • 19
  • 33
8
votes
2 answers

MockK mock ViewModel's savedStateHandle.getLiveData()

I'm trying to test this ViewModel with savedStateHandle class PostListViewModel @ViewModelInject constructor( private val coroutineScope: CoroutineScope, private val getPostsUseCase: GetPostListUseCaseFlow, @Assisted savedStateHandle:…
Thracian
  • 43,021
  • 16
  • 133
  • 222
8
votes
1 answer

Why is mocking so slow to start in Kotlin?

Can anyone tell me why mocking "startup" is so slow in Kotlin? The first test that uses mocks takes seconds (2-3 with mockk and 1-2 with Mockito). The rest take milliseconds. There is no such overhead in Java with Mockito. Way to reproduce: Write…
Paweł Krupiński
  • 1,406
  • 1
  • 14
  • 23
8
votes
2 answers

mockk java.lang.AssertionError: Verification failed: call 1 of 1: was not called

I'm running a unit test using mockk. When trying to verify a method, I'm getting an assertionError and I can't figure out how to get the test to run. Here is my test method: @get:Rule var rule: TestRule = InstantTaskExecutorRule() val…
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
8
votes
1 answer

Mockk - spyk mock method only once

I have spyk from mockk library: my = spyk(My()) later I am mocking one of the method to return something like: every { my.method("someString") } returns something I'm creating this spyk in a @BeforeAll method and I'm reusing it a few times but,…
Ice
  • 1,783
  • 4
  • 26
  • 52
8
votes
1 answer

How to test through io.mockk a method that was called several times with different parameters?

PS: Code will be in Koltin For example, I have my service class that does something and injects some other service. class MyService( private val someOtherService: OtherService ) { fun doSomething() { someOtherService.someMethod("foo") …
Seydazimov Nurbol
  • 1,404
  • 3
  • 10
  • 25
1 2
3
34 35