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

MockK's "every" "returns null" not returning "null" but a NetworkInfo object with null and false values

In my unit test I am calling the following: every { connectivityManager.activeNetworkInfo } returns null but when I debug I get a NetworkInfo object filled with null and false values networkInfo = {NetworkInfo@9780}"[type: null[null], state:…
jteichert
  • 527
  • 1
  • 4
  • 20
2
votes
1 answer

Test with Kotlin Coroutines is randomly failing

Let us suppose we have a class member whose purpose is to bring 2 objects (let's say object1 and object2) from two different places and then create the final result merging these two object in another one, which is finally returned. Suppose then the…
davide.ferrari
  • 221
  • 2
  • 10
2
votes
1 answer

How to mock LiveData in mockk kotlin

Hey I am using mockk library in my project. I am trying to mock MutableLiveData, but it always give me null value. Can someone guide me how to do in proper way. Suppose I have one function var dataLiveData = MutableLiveData() val…
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
2
votes
1 answer

How can I check the constructur arguments of a mockk Mock?

I have the following code (in Kotlin): class X { fun foo() { val A(1, true, "three") val b = B() b.bar(A) } } What I want to to is find out what A has been instantiated with. My test code looks like so: // Needed for…
MysteriousWaffle
  • 439
  • 1
  • 5
  • 16
2
votes
1 answer

Mockk Missing calls inside verify { ... } block

I have a unit test that is throwing out the Missing calls inside verify { ... } block. This unit test was running fine before I converted the PhoneNumberSelectionActivity from java to kotlin. The way it was before the conversion: @Test fun `handle…
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
2
votes
0 answers

Mockk verification of deep stubs fail

Kotlin version: 1.5 - Mockk version 1.12.0 I know that every time a mock returns a mock we are somehow violating the Law of Demeter. In my app I'm using RedisTemplate which for every operation its delegating to another object…
Vahid
  • 1,625
  • 1
  • 18
  • 33
2
votes
1 answer

Mockk set private properties of collection in Kotlin

While accessing the private variables in Mockk, not able to set the values of private property. We have CustomerImpl class and which has 1 private property called customerData. We want to set data in private property customerData from our Test case…
Nishant Shah
  • 3,442
  • 5
  • 25
  • 34
2
votes
0 answers

Class cast exception happened when resttemplate postforobject is mocked with MockK

I have an error when I mock restTemplate.postForObject method with a specific flavor of java. With java version 11.0.11.j9-adpt: o.mockk.MockKException: Class cast exception happened. Probably type information was erased. In this case use `hint`…
2
votes
3 answers

How to use mockk with a generic constructor param?

So I have a class class Test @Inject constructor( private val dependency1: Dependency1, private val listener: T ) I'm trying to write a unit test for it using mockk and running into an error when trying to mock and…
Kyle
  • 1,430
  • 1
  • 11
  • 34
2
votes
1 answer

UI Testing with Hilt

I am trying to mock a view model in my UI Tests. It seems like I don't have it set up correctly, because the VM in the test activity is "not initialized". This is what I got: Hilt Module that provides my…
lisa
  • 640
  • 5
  • 10
  • 26
2
votes
2 answers

Unit tests in kotlin with Mockk overload resolution ambiguity

I need to get the results of the query with specifications. In order to do that I'm using JpaSpecificationExecutor's List findAll(@Nullable Specification spec) method. The problem is that I can't do the same in tests, cause it has several…
Tracker7
  • 87
  • 1
  • 10
2
votes
2 answers

mock nested properties in mockk

I have a function which uses a field in DisplayMetrics of Resources of Context class: fun getIconForDevice(context: Context, iconUrl: String): String { val metrics = context.resources.displayMetrics var suffix = "" //below checks MUST be…
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
2
votes
1 answer

How can I partially verify dataclass as function argument?

I'll et the code speak. @Test fun test() { data class Activity( val type: String, val ts: Instant, ) fun persistActivity(a: Activity): Unit = mockk() fun createActivity(type: String) { …
Jen
  • 1,206
  • 9
  • 30
2
votes
2 answers

Mock a "global" property in Kotlin

Is there a way to change the return value of the property accessor called when running unit tests? Like mocking the result of the property settingsState? I am learning to create unit tests. What that class makes is to bring stored data into the…
Ricardo
  • 91
  • 5
2
votes
1 answer

Android testing view model with mockK

I'm trying to test my view model with mockK library. but i can not figure it out how to do that. This is my class. I have a use case and a repository: @ExperimentalCoroutinesApi class MainViewModelTest { private val…
Shadman Adman
  • 383
  • 3
  • 12