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
8
votes
2 answers

Test LiveData and Coroutines using MockK

I have this view model: class MyViewModel(private val myUseCase: MyUseCase) : ViewModel() { val stateLiveData = MutableLiveData(State.IDLE) fun onButtonPressed() { viewModelScope.launch { stateLiveData.value =…
Milack27
  • 1,619
  • 2
  • 20
  • 31
8
votes
2 answers

How to inject a list of implementations in a @InjectMockKs test instance?

Spring Boot allows to inject a list of all implementations of an interface (SomeComponent) as List into another component (SomeOtherComponent), e.g. @Component interface SomeComponent @Component class SomeComponentImpl0 :…
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
8
votes
2 answers

Can't mock android.util.Patterns.EMAIL_ADDRESS.pattern()

Currently I'm using MockK library (version 1.8.1) for unit tests in Android Dev, and I the problem is I can't mock Patterns.EMAIL_ADDRESS. Test cases throw NPE every time this property gets invoked. I tried mockkStatic(Patterns::class), but…
7
votes
5 answers

Android Studio Arctic Fox unable to run JUnit configuration with mockk

I just upgrade my Android Studio to the latest one. And currently I guess some of my test classes producing flaky test result and I want to run repeat mode in Android Studio as I did in Android Studio 4.2. But I found that I got the following…
Long Ranger
  • 5,888
  • 8
  • 43
  • 72
7
votes
1 answer

MockK spy on top-level private function in Kotlin

I need to verify if bar function is called or not using MockK library. MyFile.kt fun foo() { bar() } private fun bar() { ... } How can I mock the 'bar' function? I am trying the following. @Test fun test() { …
Ashwin
  • 7,277
  • 1
  • 48
  • 70
7
votes
0 answers

How to verify deep equality of arguments passed to a Mockk mock?

As it seems, Mockk mocks only store the reference to the received arguments for later verification. This poses a problem as soon as the argument becomes modified throughout the test. How could one verify a mock function invocation for deep equality…
Heiko
  • 149
  • 1
  • 9
7
votes
1 answer

Android Local Unit Test - Mock FirebaseAuth with MockK

Overview Expected Behavior - Mock FirebaseApp.class in a local unit test with JUnit5 in Android using MockK's class, relaxed, or constructor mock features. Current Behavior - The following error is thrown. java.lang.NoClassDefFoundError: Could not…
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
7
votes
1 answer

Unit Testing with mocking of FusedLocationProviderClient and dependency injection with Koin

Within our Android app we are using Google Maps API in order to show the users location. At the moment we are using Koin in order to provide a parametered injection (it requires an activity) of the FusedLocationProviderClient dependency into our…
7
votes
3 answers

How to mock a POST request with mockk and fuel in kotlin?

I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that. val client = mockk() every {…
user3745870
  • 321
  • 1
  • 5
  • 13
6
votes
0 answers

Mockk spied class with anonymous class field references original (non-spied) object

When using Mockk to spy a Kotlin or Java class, if the class has a field that is an anonymous class (like View.OnClickListener), when the field functions reference the parent class, it is not reflected on the spied object. Example Class with…
the.joeba
  • 376
  • 3
  • 7
6
votes
1 answer

Capture vararg arguments with MockK?

You can mock a vararg method in MockK: interface ClsWithManyMany { fun manyMany(vararg x: Any): Int } val obj = mockk() every { obj.manyMany(*anyVararg()) } returns 2 println(obj.manyMany("testing", "testing")) // 2 How can I…
Cristan
  • 12,083
  • 7
  • 65
  • 69
6
votes
1 answer

Mockk spky throwing NoClassDefFoundError

I am trying to write a unit test for my view model with help of Mockk. @Test fun `When loading the ResponseViewState isLoading`() { val observer = spyk>(Observer { }) …
Mehdi Satei
  • 1,225
  • 9
  • 26
6
votes
1 answer

How to mock a builder using MockK so that the builder functions are commutative?

I got some code which I want to test using MockK as a mocking library: fun loadImage(imageLoader: coil.ImageLoader) { val request = ImageRequest.Builder(imageView.context) .data(url) .crossfade(true) .build() …
Henning
  • 2,202
  • 1
  • 17
  • 38
6
votes
0 answers

How to properly mock properties that will be used in init block?

When we create a new class A in test it inits our values and in the init block that values are passed to the class B, only after that the values getters are mocked. The main problem is that I want to pass the mocked values to class B in init…
Mark D
  • 95
  • 2
  • 8
6
votes
1 answer

Kotlintest not executing test when using springmockk

I tried to write an integration test for my kotlin spring application. For this I am using the kotlintest framework. As I need to mock one of the beans in my application I also added mockk with the springmockk extension. After adding the springmockk…
JJensen
  • 86
  • 5