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

Does mockk support suspend inline?

It looks like coEvery hangs when I am trying to mock suspend inline function. The code below works if remove inline modifier Function in storeApi: suspend inline fun getAllStores(): List Test code: coEvery { storeApi.getAllStores() } returns…
5
votes
1 answer

How to mock sealed classes with mockk?

I am trying to mock a sealed class which looks something like this: sealed class Location class Home: Location{ val name = "Home" } I would like to be able to do the following: val mockHome = mockk() { every { name } answers { "My…
Spaci Tron
  • 383
  • 1
  • 4
  • 11
4
votes
1 answer

How is mockk's allAny() used

I can't find any documentation on allAny() that I can understand. The official documentation describes it as a "special matcher that uses any() instead of eq() for matchers that are provided as simple arguments". I don't understand what that…
k314159
  • 5,051
  • 10
  • 32
4
votes
1 answer

How to mock function without class in Kotlin?

fun add() { return 4+1; } class Calculator { fun MathUtils() { // do something // calls add() function val x: Int = add() // return something return x + 22 } } class CalculatorTest { var c…
Ayush Naithani
  • 103
  • 1
  • 5
4
votes
0 answers

How to mock intent using Mockito?

I am attempting to mock intent inside the function I made. Here is the function below fun learningUnitTest(context: Context) { val str = context.getString(R.string.app_name) val intent = Intent(context, TodoFragment::class.java).apply { …
Marfin. F
  • 434
  • 4
  • 16
4
votes
1 answer

I am trying to use mockk anyConstructed but ending with Missing calls inside every { ... } block

I am trying to return an object whenever a new object of a class is being created. I have tried using anyConstructed with a spyk or even mockk object of PredictionCheckFunction every { anyConstructed() } answers {…
Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
4
votes
1 answer

how to test equality of data class with OffsetDateTime attribute?

I have an attribute in a DTO and Entity defined like this: val startDate: OffsetDateTime, The dto has a toEntity method: data class SomeDTO( val id: Long? = null, val startDate: OffsetDateTime, ) { fun toEntity(): SomeEntity { …
Stuck
  • 11,225
  • 11
  • 59
  • 104
4
votes
1 answer

how to resolve Suspension functions can be called only within coroutine body

I am trying to use MockK in my project, when I try to verify an api call in the interface I get an error Suspension functions can be called only within coroutine body this is my code import com.example.breakingbad.api.ApiServiceInterface import…
BRDroid
  • 3,920
  • 8
  • 65
  • 143
4
votes
1 answer

io.mockk.MockKException: Failed matching mocking signature for SignedCall

I'm facing an issue with running mocked tests (using mockK & Kotlin). I had a couple of working unit tests. Yesterday I was testing the feature I implemented. But whenever I try to run the tests, this exception appears: io.mockk.MockKException:…
4
votes
1 answer

MockK: verify that a function is called with a parameter of specific subclass type of a parent class

I am using MockK for testing. Language: Kotlin. I have a sealed class A with a few subclasses, say B, C, and D, which is used as a parameter for a function call. I need to make sure that a function is called with one of the subtypes of A. For that I…
Sermilion
  • 1,920
  • 3
  • 35
  • 54
4
votes
2 answers

Android test Fragmen with mock ViewModel using Hilt

I´m developing an app using Hilt, all works fine but when I try to run some Espresso test on a device running on below Android P I have encountered an issue. The problem comes when I try to mock (using Mockk) the ViewModel so I can unit test my…
JJaviMS
  • 392
  • 6
  • 18
4
votes
0 answers

Kotlin mocking a var with get/set using Mockk

I have this Kotlin class that is a wrapper around SharedPreferences in an Android app. class Preferences(private val context: Context) { private val preferences: SharedPreferences = context.getSharedPreferences("name_of_file",…
Daniel Bejan
  • 1,468
  • 1
  • 15
  • 39
4
votes
1 answer

Kotlin: How to verify an extension function is called on a mock

Say I have a Java class Metrics. I defined some extension functions on Metrics in Kotlin fun Merics.expose(name: String, value: Number) { // do something } Note that the Java class Metrics also has a method called expose but with different…
Rich
  • 1,669
  • 2
  • 19
  • 32
4
votes
1 answer

Json parser using Kotlin Data Class correctly returns json data, but why does unit testing of parser (MockK) fail?

I have a very simple JSON parser that uses Kotlin data class, and it works perfectly. However, when I try to unit test the parser method (MockK), assertion fails as the model object returns NULL values. I am new to both Kotlin and MockK, so please…
AdiKay
  • 41
  • 2
4
votes
2 answers

mock common tests in kotlin using multiplatform

I can't use the common mock library ( mockk.io ), with kotlin multiplatform. In their website it says that to use mockk in kotlin multiplatform you just need to add this line to your gradle. testImplementation "io.mockk:mockk-common:{version}" I…
saykou
  • 167
  • 1
  • 9