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

Mockk : Mock more than one interface at the same time

I'm trying to mock some interfaces using Mockk. At some point, I have to create a mock that implements 2 interfaces. For instance, using Moq in C#, I can do that : // implementing multiple interfaces in mock var foo = new Mock(); var…
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
3
votes
1 answer

How to mock internally constructed instance with mockk.io?

I am writing unit test case in mockk and Junit5 for a static method defined in companion object of FileUtility class. the method is as below, class FileUtility { companion object { fun getFileObject(fileName: String): File { …
Akash Patel
  • 189
  • 1
  • 5
  • 13
3
votes
1 answer

Kotlin Mockk test for suspend cancellable coroutine cancellation

I have classes // final class from some library like okhttp class NetworkCaller { fun call() { // performs some real operation } fun cancel() { // .... cancels the request } } class Request { suspend fun…
Minion
  • 964
  • 14
  • 16
3
votes
3 answers

ArgumentMatchers in MockK

I'm converting from Java to Kotlin, and from Mockito to MockK. I'm stuck at converting Argument Matchers from Mockito to MockK. Mockito can do any() to match anything, including nulls and varargs. (imports ArgumentMatchers.any) For example:…
Humble Hermit
  • 185
  • 2
  • 14
3
votes
2 answers

How to test Intent's Bundle using Mockk?

I would like to test a custom handler class which is starting a new Activity. I would like to test the Intent's Bundle if contains the pre defined parameters. The test class: @MockK lateinit var activity: ActivityCalendar @Before fun setUp() { …
Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
3
votes
1 answer

Kotlin and MockK throw com.sun.tools.attach.AttachNotSupportedException: no providers installed

Using Zulu JDK (8u242b20) in an Java application, I'm receiving an exception when trying to run a test from a kotlin class as follows: class SomeTest { @MockK private lateinit var fileService: FileService @SetUp fun setUp(){ …
Michael Bláha
  • 383
  • 4
  • 9
3
votes
1 answer

Mockk: Verify method called within coroutine

I have a simple object which provides a suspend function to simulate a delaying network request and afterwards calls another method from the object. class CoroutinesObject { suspend fun doApiCall() { delay(1000) println("Hello from API") …
jennymo
  • 1,450
  • 1
  • 18
  • 43
3
votes
1 answer

Mockk current time

I am using MockK for Unit-Tests and want to verify if some function was called with current time(milliseconds) parameter. In main app the function call: functionName(System.currentTimeMillis()) But in test: verify(exactly = 1) { …
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
3
votes
0 answers

How to wait until io.mockk captured slots will be filled while calling a method that contains asynchronous operations? (Unit Test)

PS: Codes in Kotlin In my unit test, I have mutable list of string called slot val slot = mutableListOf() I captured this slot list every { someService.whateverMethod(capture(slot)) } just Runs In the end of certain test I check size of…
Seydazimov Nurbol
  • 1,404
  • 3
  • 10
  • 25
3
votes
1 answer

Mockk in Kotlin: Argument passed to verify is not a mock

I have defined my mock as follows: private val dal = mockk { every { insert(any()) } returnsArgument 0 } Then, I'm trying to test it like this: @Test fun test() { userService.registerUser(userJohn) verify(dal).insert(check { …
Whizzil
  • 1,264
  • 6
  • 22
  • 39
3
votes
1 answer

Mockk calls the method instead of mocking the dependency

Hi I have the something similar in my production code. A simple method that will throw an exception: class Production { fun doWork(): String { throw IllegalArgumentException() } } However, when I mock this Production class with…
blackpanther
  • 10,998
  • 11
  • 48
  • 78
3
votes
1 answer

MockK's spyk how to overwrite constructor?

I currently try to test a class as a spy object that normally initialize a database connection in the constructor. The a simplified version of the class looks like this. class classToTest(){ val connection:Connection init { …
Bierbarbar
  • 1,399
  • 15
  • 35
3
votes
2 answers

How to mock android room withTransaction method with Mockk

I'm trying to make some unit tests for my business logic. I have repository in which I save to room database (2.1.0-rc01) some data from response. Data saving into different tables with different dao in single transaction. Code is…
vvg
  • 43
  • 1
  • 4
3
votes
1 answer

Trying to convert this Mockito test to Mockk results in error

Having issues with the following conversion from Mockito to Mockk. So I have this @Mock private lateinit var loginLiveDataObserver: Observer val inOrder =…
Crash1hd
  • 615
  • 7
  • 23
3
votes
1 answer

How to compare or verify object that does not override equals method?

I use mockk for unit testing in Kotlin (Android). I want to verify that a function is called: verify { obj.callSomething("param1", Param2("A", "B")) } In this case Param2 is a generated Java class that doesn't override equals method so that the…
fikr4n
  • 3,250
  • 1
  • 25
  • 46