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

MockK - mock / spy top level extension method being called on top level val

In the MWE below I'm trying to verify that calling baz() also calls a method on another object. However, I can't seem to mock / spy on that object. MWE: package com.example import io.mockk.every import io.mockk.mockkStatic import…
Erik
  • 4,305
  • 3
  • 36
  • 54
2
votes
1 answer

How mock private method with nullable args

I'm use MockK for unit test. How i can mockk private call with nullable and not nullable args? My method: private fun trySaveLogin(session: Session, login: String, passwordHash: String?, passwordHashNoSalt: String?, userInfo: UserInfo) { //…
darjke
  • 21
  • 1
  • 3
2
votes
2 answers

How to use MockK to mock an observable

I have a data provider that has an Observable as part of the public API. My class under test maps this into a Observable. How do I create a mock so that it can send out different values on the data provider's observable? I can do it…
Jim Leask
  • 6,159
  • 5
  • 21
  • 31
2
votes
1 answer

How to mock and test RxJava/RxAndroid with Mockk?

I want to mock and test my Presenter with the Observable, but I don't know how to do that, the main part of the code as below: //in my presenter: override fun loadData(){ this.disposable?.dispose() this.disposable = …
SpkingR
  • 965
  • 7
  • 14
2
votes
0 answers

Spyk with generic

I want to use spyk for class with generic. It produces StackOverflowError. It is impossible to use every { childClazz.foobar(view) } just Runs and super.foobar(view) definitely need to be invoked class SpyTest{ @Test fun callFoobar(){ …
2
votes
1 answer

How mock Kotlin extension function in interface?

I have an extension function for interface like the following: import javax.jms.ConnectionFactory fun ConnectionFactory.foo() { println("do some stuff") } How can I mock the function foo? Please note, I have seen approaches for classes and…
NameAlex
  • 163
  • 1
  • 3
  • 10
2
votes
1 answer

How to mock android.util.Patterns with Mockito or MockK

I have a method that I need to test : fun validate(email: String): Result { return if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { Result(true) } else { Result(false, "error") } } But it returns a…
BakaOne
  • 126
  • 1
  • 9
1
vote
0 answers

Junit5 concurrent execution mode on a class impacting another test class?

I have a Kotlin project, using JDK 17, Junit 5.10.0, and junit-platform 1.10.0 (the latest Junit versions as of today) with several thousands of unit tests. Because of some framework choices we made, we are forced to run tests in sequential mode:…
Vincent F
  • 6,523
  • 7
  • 37
  • 79
1
vote
1 answer

mockk, why it cannot stub the PackageInfo.versionName

Tryin to mock the android.content.pm.PackageInfo, and stub the versionName. @Test fun test_() { val pInfoMock = mockk() every { pInfoMock.versionName } returns "test_version". //<==got error // ...... …
lannyf
  • 9,865
  • 12
  • 70
  • 152
1
vote
0 answers

Cannot mock Android drawable using @Mockk annotation?

I want to mock an Android drawable for some unit tests. I'm using the @Mockk annotations. This is the code: @MockK lateinit var mockIcon: Drawable @Before fun setUp() { MockKAnnotations.init(this) } When I run this code, I get this error…
Toni Joe
  • 7,715
  • 12
  • 50
  • 69
1
vote
0 answers

Fail to mock jOOQ 3.18 classes with MockK, works with 3.14

I work on a code basis written in Kotlin that uses jOOQ to interact with databases. It uses jOOQ to generate the "meta model" i.e. classes abstracting tables, fields, etc. jOOQ generates those classes in Java. Our unit tests mock some of the jOOQ…
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
1
vote
1 answer

MockK cannot resolve Generic T from method from abstract class extended

All my services extend one abstract class DefaultBaseService, ID> with some basic CRUD methods like: override suspend fun update(id: ID, obj: T): T { obj.id = id return save(obj) } override suspend fun update(id: ID, map:…
Arthur
  • 11
  • 2
1
vote
0 answers

Controller test is failing due to MockkException as no answer is found for the mocked service method

I'm testing my controller containing two endpoints, one for signup and another for signin. Signup is the one failing while signin succeeds. Here's the stacktrace Request processing failed: io.mockk.MockKException: no answer found for:…
Menyten
  • 41
  • 2
  • 5
1
vote
1 answer

How to test coroutines with await() in Kotlin?

I'm trying to create tests with mockk for my suspend functions that include await(). I have tried the following code so far, but the test never ends. private lateinit var repository: AuthRepository private lateinit var databaseReference:…
Kleini
  • 57
  • 7
1
vote
3 answers

mock private constructor and its multiple parameters with mockk

I want to mock the private constructor of a class with its multiple parameters (with Kotlin): public final class Foo { public static class Bla { public final CustomType1 property1; public final CustomType2 property2; …
Martin
  • 11
  • 3