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

How to spy on a class constructor from an external dependency in Mockk?

Say I have a unit under test like this: internal class MySerializer { fun serialize(): ByteArray { val initialBufferSize = 1000 val autoResizeBuffer = true val value = ThirdPartySerializer(initialBufferSize,…
Gus
  • 1,132
  • 13
  • 21
0
votes
1 answer

Mockk: Execute callback code in real object calling a mockk service

I try to achieve something similar to this: How to call a lambda callback with mockk I pass a mocked service to my real object. How can I get the myService.get callback called? This code gets never called and my test stops at this request. Here is…
devz
  • 2,629
  • 2
  • 31
  • 37
0
votes
0 answers

Kotlin Micronaut Mockk no answer found exception

I am new to the Micronaut framework and trying to test a demo example I created. I am encountering the error io.mockk.MockKException: no answer found for: MathFacade(#3).computeAgain(3). My code works as such - MathService invokes MathFacade. I…
0
votes
1 answer

JUnit 5 Annotations - Not Working with LiveData / Parameterized Tests

Overview Expected Behavior Replace mock object initialization using the mockObject function implementation with annotation syntax initialization for JUnit 5 as outlined in the documentation and Medium post by @oleksiyp. Current Behavior The test in…
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
0
votes
1 answer

blocked on writing unit tests to a presenter that has BoxStore injected

So here is the overview of my project: module A contains: - all boxstore data - boxstore mock for unit tests in module A module B contains: - presenter that has BoxStore injected - presenterTest needs to mock BoxStore Followed this link to mock…
Karim Fikani
  • 356
  • 3
  • 23
0
votes
1 answer

Android: How to unit test Observable concatMap compose with mockK

I need to unit test an Observable with concatMap and then compose. I'am using RxJava and mockk library. I've been trying to use two separetes every statements to mock the results. But when i call first every { …
0
votes
2 answers

Kotlin Spring WebMvcTest Mockk

Has anyone encountered the issue where @MockkBean does not seem to actually work or at least the stubs are not coming through? The simple example: @RunWith(SpringRunner::class) @WebMvcTest(controllers = [WidgetController::class]) class WidgetTest…
echen
  • 2,002
  • 1
  • 24
  • 38
0
votes
1 answer

Random Test Crash TDD Kotlin

The following test below causes a random crash. I am at a loss most of the time it works fine. > @Test fun `perform login without a previously saved user`() = > runBlocking { > testSavedUser1LiveData = MutableLiveData() > > coEvery {…
Crash1hd
  • 615
  • 7
  • 23
0
votes
0 answers

How to unit test functions that just call a dependency's function that returns nothing?

I'm having problems on how to unit test my view model function. class MyViewModel( private val dependency: Dependency ) : ViewModel() { private val _myFunctionState = MutableLiveData() val myFunctionState: LiveData =…
0
votes
0 answers

gradle(compileTestKotlin) fail about io.mockk.every

I'm newbie about Kotlin, Gradle. i tried to TDD for rest api(controller) test. i saw https://spring.io/guides/tutorials/spring-boot-kotlin/ that help kotlin tdd beginner. however, something wrong. error message CouponControllerTest.kt: (7, 8):…
unilep
  • 313
  • 2
  • 9
0
votes
1 answer

Where to put the MockK settings.properties file in an Android project?

We're using MockK for unit testing our Android project. Since we're almost always use mockk(relayed=true) we wanted to set it as default in the settings.properties as suggested in the docs. It says: "create io/mockk/settings.properties file in…
ASP
  • 773
  • 1
  • 8
  • 22
0
votes
0 answers

How to mock a Bitmap using mockk without it affecting bitmap.compress in future tests

How do I mockk a Bitmap without it breaking Bitmaps in future tests? Let's take the following simplified example... class FileHelper @Inject constructor(private val application: Application) { fun saveBitmapToTempDirectory(bitmap: Bitmap,…
JHowzer
  • 3,684
  • 4
  • 30
  • 36
0
votes
0 answers

How to mock Class for JUnit Test

How can I mock Class ? I get therer error : Mockito match any class argument when I try to mock and use it in a whenever(mockClass.method(mockClass)).thenReturns(...) any(Class::class) or any(Class::class.java) didn't help either.
BestPractice2Go
  • 285
  • 4
  • 16
0
votes
1 answer

how to mock calling kotlin.system.exitProcess

I want to test some code that uses 3rd party code that calls kotlin.system.exitProcess(), defined as follows in the standard lib: @kotlin.internal.InlineOnly public inline fun exitProcess(status: Int): Nothing { System.exit(status) throw…
tchick
  • 357
  • 4
  • 14
0
votes
1 answer

Cast exception when mocking kotlin lambda callbacks in Mockk

I am having some trouble mocking callback functions with Mockk. I am trying to mock a task success listener that is called like this: collection .add(Item()) .addOnSuccessListener { update(collection.document(it.id)) } Where the…
CampbellMG
  • 2,090
  • 1
  • 18
  • 27
1 2 3
34
35