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
1
vote
1 answer

Mock method call and return value without running the method (MockK)

I saw similar post: How to mock method call and return value without running the method? I am wondering if there is similar method we can use in mockK? something like: doReturn.when(mock).method in mockito I saw this question before Equivalent of…
dosu
  • 41
  • 2
  • 8
1
vote
0 answers

Mocking scope function in Kotlin

I am using following object creation methods in kotlin class Resource( referenceFactory: ReferenceFactory, id: String, name: String, role: String, odi_runs: int, …
1
vote
1 answer

How to mock a class of a library used by the app during instrumented test?

I am working on an Android library and I am writing an instrumented test for it using UI Automator and Mockk. The library has a class called InstallManager which will install stuff on the device. I want the InstallManager to throw an exception so…
1
vote
1 answer

Mock throwing of Exception with Android Coroutines

I am introducing coroutines to an android app I am working. There is lots of legacy but I am now adding some new networking code and using coroutines for it. EDIT Updating to add the whole test coEvery { service.getAddress(any(), any(), any()) }…
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
1
vote
1 answer

Cannot Capture Mutable List Mockk

Kotlin error when compiling such code mentioned in https://mockk.io/#capturing. What's wrong actually? class Foo{ fun fx(parm: MutableList) {} } val foo = Foo() val parm: MutableList = mutableListOf() every {…
Varid Vaya
  • 194
  • 1
  • 15
1
vote
1 answer

Kotlin Mockk a getter with parameters

My code is extending Spring Boot's Authentication class with properties derived from the JWT. For example: import org.springframework.security.core.Authentication ... val Authentication.role: List? get() = (principal as? Jwt)?.let { …
stakolee
  • 893
  • 1
  • 7
  • 20
1
vote
1 answer

Using RabbitListenerTestHarness with Mockk

We want to write tests for a project that uses Spring Boot and Spring AMQP. As we code in Kotlin we would like to use MockK instead of Mockito as it better fits Kotlin code style and best practices. The RabbitListenerTestHarness class provides some…
Rosso
  • 428
  • 7
  • 17
1
vote
0 answers

Unit test multiple LiveData values in a ViewModel init block

Consider the following code: sealed interface State { object Loading : State data class Content(val someString: String) : State } class SomeViewModel(getSomeStringUseCase: GetSomeStringUseCase) : ViewModel() { private val _someString =…
dsantamaria
  • 188
  • 1
  • 8
1
vote
1 answer

Error inflating ViewBinding in test class : Binary XML file line #38: Binary XML file line #38: Error inflating class

I am trying to write unit tests for a RecyclerView.ViewHolder class which uses ViewBinding but I am facing issues to inflate my ViewBinding in my test class, having this error when running my test : Binary XML file line #38: Binary XML file line…
Alan
  • 1,264
  • 1
  • 11
  • 21
1
vote
0 answers

How to unit test responses of FutureCallback with ApacheAsyncHttpClient

I am trying to figure out how to unit test the callback methods of a FutureCallback, but can't quite figure it out. I have the following code: val latch = CountDownLatch(10) httpClient.execute(httpPost, object:…
1
vote
1 answer

How to mock local variables using MockK

I have the following simplified code class Foo() { suspend fun bar() { val headers = AtomicReference(Metadata()) val metadata = headers.get() if (metadata.keys().size > 0) { // I want it to return a value specified in the…
Trumango
  • 17
  • 1
  • 5
1
vote
1 answer

MockK creating invalid objects using a private constructor

Given a wrapper class around a foreign pointer: class CObject private constructor(private val _internalCPointer: Long) { external fun doACThing() companion object { external fun allocate(): CObject } } mockK is generating…
cobbal
  • 69,903
  • 20
  • 143
  • 156
1
vote
2 answers

How to test async function in Mockk Kotlin

I want to test this function. suspend fun fetchTwoDocs() = coroutineScope { val deferredOne = async { fetchDoc(1) } val deferredTwo = async { fetchDoc(2) } deferredOne.await() …
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
1
vote
0 answers

mocking a private function with suspend higher order function as argument with Mockk

Actually issue is, i am unable to mock a private function due to suspendable lambda as arguement (val condition: suspend () -> AppResult) So, in that regard i need your assistance. Code to be tested, class UserPlayListManagerImpl( private…
Reprator
  • 2,859
  • 2
  • 32
  • 55
1
vote
2 answers

Mockk mocking Java "enum" singleton

I'm working in a project which has some Java legacy code. One common pattern there is to create singletons using the "enum" approach: enum MySingleton { INSTANCE; int answer; init { answer = 42; } int fun1() { return answer…