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
3
votes
0 answers

How to mock completable of RxKotlin using MockK(Mock Framework)

For unit testing in kotlin android, I have been using the mocking framework MockK. Have used RxKotlin. I have used Completable observable to notify the ViewModel about the status of API. Based on the completable, the status will be updated to…
Raghul Vaikundam
  • 588
  • 4
  • 20
3
votes
1 answer

Kotlin MockK: io.mockk.MockKException: no answer found for

This is my code snippet: @Test fun `request should return anon id if query param present`(@MockK(relaxed = true) req: ServerRequest) I'm using JUnit5(Jupiter). The exception I've got is: io.mockk.MockKException: no answer found for:…
yuranos
  • 8,799
  • 9
  • 56
  • 65
3
votes
1 answer

mockk verify after a delay

Whats the best way to verify after a delay? For instance, I make a method call that calls postDelayed on some other object, and the call I want to verify happens in that run() block. For instance, with Mockito, you can do the…
Tyler
  • 19,113
  • 19
  • 94
  • 151
3
votes
1 answer

Mocking OffsetDateTime.now with mockk

Trying to mock OffsetDateTime.now() with mockk, but it throws Missing calls inside every { ... } block I tried: staticMockk().mock().run { val mockTime = OffsetDateTime.now() every { OffsetDateTime.now() } returns mockTime //…
Razor Storm
  • 12,167
  • 20
  • 88
  • 148
2
votes
1 answer

Mockk: Stubbing same function twice ignores first behaviour

I want to provide a null value for all calls with an id >= 100 -- and a concrete Product instance with the captured id for all others: every { repoMock.findById(more(100, true)) } returns null every { repoMock.findById(capture(idSlot)) }…
Tom
  • 1,965
  • 3
  • 25
  • 33
2
votes
1 answer

What is the difference between verify(exactly = 0) and wasNot Called in Mockk?

What is the difference between using verify(exactly = 0) and using wasNot called assertions of MockK when testing Kotlin? I have an example where the former passes the test but the latter yields: java.lang.AssertionError: Verification failed: call 1…
Balu
  • 522
  • 6
  • 16
2
votes
2 answers

Mockk with context receiver

I'm trying to use Mockk to mock a method with context receiver: class MyClass { // The method I'm going to mock context(CallContext) fun myMethod(a: Int) Int { a } } It's hard to get the instance of CallContext in the unit test. So I…
ffcactus
  • 152
  • 8
2
votes
2 answers

Mock a constructor and return a mocked object instead of real object with mockk

I have a challenge with a class which I want to test but inside the class other objects will be created. This simple example shows the issue. class A { val b: B init() { b = B() } } It's just an example and I know that dependency…
axstel
  • 111
  • 5
2
votes
1 answer

In Kotlin MockK What Will Happen After unmockkAll?

What exactly does unmockkall method do? testImplementation "io.mockk:mockk:1.12.8" @Test fun test1() { val mockEx: Example = mockk() every { mockEx.foo() } returns "mocked" unmockkAll() val res = mockEx.foo() println("res =…
Ashwin
  • 53
  • 5
2
votes
1 answer

Android kotlin unit test - Exception in thread "Test worker @coroutine#4" io.mockk.MockKException: no answer found for: View(#1)

I'm writing the unit test of a presenter that fetches data from an API : class SearchPresenter constructor( private val view: SearchContract.View, private val coroutineScope: CoroutineScope, private val dispatcherProvider:…
user3884677
  • 413
  • 5
  • 26
2
votes
0 answers

android, how to use mockk to mock BuildConfig.DEBUG

using mockk for unit test, and would like to mock the BuilConfig.DEBUG. io.mockk.mockkObject(BuildConfig::class) // or mockkStatic io.mockk.every { BuildConfig.DEBUG } returns true //<=== throws but it throws exception Missing…
lannyf
  • 9,865
  • 12
  • 70
  • 152
2
votes
2 answers

mockkStatic for extension method with generic parameter: "Not enough information to infer type variable T"

FYI: I'm currently using Mockk-1.12.4 and Kotlin-1.6 I've got an extension method that returns an object of type T: fun Entity.selectReferenceAsSingleObject(referenceName: String): T { return…
Bradley Gray
  • 337
  • 3
  • 10
2
votes
0 answers

Execution failed for task ':app:testDebugUnitTest'. > No tests found for given includes: [com.*](filter.includeTestsMatching)

I'm trying to migrate a project from JUnit 4 to 5. The project has had a lot of migrations lately, including the swap from Groovy DSL to Kotlin DSL, Mockito to Mockk and from 'legacy' xml to JetPack Compose. However, when I try to migrate to JUnit…
Bart P
  • 21
  • 2
2
votes
1 answer

How can I mock a nested @Composable in jetpack compose?

I'm attempting to run non-instrumented tests using jetpack compose and robolectric. I've had some success in basic testing, for the most part tests work like their instrumented counter parts, but can't figure out how to mock a nested @Composeable…
2
votes
2 answers

Mockk matching and overloaded function withArg

Hello I am trying to find a way to match an overloaded function inside of the verify using withArg The doc doesnt really point this out every { getResponse.Ids } returns listOf(121212L) assert( client.getExtIds(Ids) ) verify { …
Bao Thai
  • 533
  • 1
  • 6
  • 25