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
1
vote
0 answers

Unit Test private methods in KMM project's commonTest module

I'm trying to verify the shared module's private methods with Mockk. In the AndroidApp or anroidMain module, we could mark the private methods with @VisibleForTesting from androidx.annotaion to do the unit test. What dependency should I add to the…
neo
  • 618
  • 1
  • 10
  • 29
1
vote
1 answer

java.lang.AbstractMethodError When trying to capture lambdas in unit test using mockk

I've been having some trouble making some unit tests for old code from another company. I think I almost got them working but I'm getting an AbstractMethodError when I run the test. The code snippet that is failing is the following: val…
1
vote
1 answer

Cannot see log from unit test in Android Studio

I'm working on an Android app written in Kotlin. I would like to use Log.* to see the output of some very simple logic in a unit test, rendered as string. I'm using mockk as suggestested in this other question. I don't get any errors, the test works…
Fiordarancio
  • 63
  • 1
  • 9
1
vote
2 answers

Kotlin Mockk : Unable to mock a list correctly

I am trying to verify that .shuffled() on a list is called, but get an error on running because of a prior .take(6) call on the list, and I cannot see a way around this. Here is some code that gets the same error: val mockList = …
theresawalrus
  • 347
  • 2
  • 19
1
vote
0 answers

mockK and NOT debuggable build - crashes with java.lang.SecurityException

Faced with the following task: trying to run android ui tests on a minified, not debuggable build. And I have a question: can mockK even work correctly on build with isDebuggable = false flag set? I'm trying to run it now and catching: Caused by:…
1
vote
0 answers

Why is Mockk failing and calling real method when doing coVerify(exactly = 0)

I have recently started programming in Kotlin and for writing unit test I am using Mockk library. I am stuck at a particular point where Mockk fails while mocking a generic class. I have a class called KafkaService which looks like this and this is…
ashwini abhishek
  • 206
  • 5
  • 12
1
vote
0 answers

Why is mockk executing a method when I'm checking for not invoked scenario?

I have a scenario like fun someMethod() { try { val response = callApi.method() val output = processResponse(response) } catch (throwable: Throwable) { } } Now I'm testing for the scenario when callApi.method() throws an…
AndroidDev
  • 5,193
  • 5
  • 37
  • 68
1
vote
1 answer

Why am I getting "java.lang.NoClassDefFoundError: Could not initialize class io.mockk.impl.JvmMockKGateway" when using quarkusDev task in IntelliJ?

I am using Gradle 7.5, Quarkus 2.12.3 and mockk 1.13.3. When I try to run quarkusDev task from command line and then start continuous testing (by pressing r), then all tests pass OK. However, when I do the same as from IntelliJ (as gradle run…
Martin Stangel
  • 311
  • 3
  • 10
1
vote
1 answer

Mockk verify ask a function to run inside a coroutine body

I was learning about Unit Testing with Mockk library, and the function i was testing is using a verify. So i run the test with coroutine test runTest{} but verify method ask me to run inside a coroutine body And, here is the code for function…
lelestacia
  • 201
  • 2
  • 9
1
vote
1 answer

What is the analogue of Mockito.verifyZeroInteractions(obj) in the Mockk library?

I want to switch to Mockk, but i cant find analogue of this method in Mockk It doesn't work verify (exactly = 0) { obj }
KramRus
  • 13
  • 3
1
vote
0 answers

Mock Ktor's http client with MockK

I want to mock requests with ktor's http client using MockK. The problem is all the methods related to making requests with the client are inline, so I cannot use coEvery on those methods. The next thing I tried was to go through the called methods…
1
vote
0 answers

Abstract Method Error while UI testing in Compose with MockK

I'm trying to test WorkoutScreen with the following code block. I created a function called setView to test ViewState in different situations. @HiltAndroidTest @RunWith(AndroidJUnit4::class) class WorkoutScreenTest { @get:Rule(order = 0) …
1
vote
1 answer

Kotlin & MockK - mocking not working if a mocked method is called from another method

I have a problem with MockK. I have a class: @Service class ItemServiceImpl(private val varPuObjectMapper: VarPuObjectMapper) : OutboundAdvicesService { override suspend fun getItemsForWarehouse(warehouseId: String): ItemsDTO { // do…
hc0re
  • 1,806
  • 2
  • 26
  • 61
1
vote
0 answers

How to @Inject a set of modules into a unit test with @ProvidesIntoSet annotation

I am trying to unit test the following. I have a class as below - @Singleton class A @Inject constructor( private val dependency1, private val dependency2 ) { @Inject(optional = true) val handlersSet: Set? = null .... } This…
xyzzz
  • 1,463
  • 5
  • 18
  • 28
1
vote
2 answers

Unit Testing: Verify that a method was called, without testing frameworks like Mockito or MockK

Not using testing frameworks like MockK or Mockito seems to be becoming more and more popular. I decided to try this approach. So far so good, returning fake data is simple. But how do I verify that a function (that does not return data) has been…
Sermilion
  • 1,920
  • 3
  • 35
  • 54