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

Kotlin, MockK: How to verify that a method call throws an exception with MockK?

I am learning Kotlin and MockK. I have seen how to verify that a method was called and how to check the result. But how would I check, that the method has thrown an exception?
DanielBK
  • 892
  • 8
  • 23
2
votes
1 answer

Mockk: How to mock object id being generated when object is saved (answers + vararg)

In my test I need to mock a situation, when new object is saved into database using entity manager (em) and during that process, id property of that object is set to auto-incremented id of that row in database. I would like to set that id property…
Aleš Zrak
  • 603
  • 2
  • 10
  • 19
2
votes
1 answer

How to mock super method by MockK

Assuming we have Activity.onCreate() like this class MyActivity : AppCompatActivity() { fun doSomething() {} override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) doSomething() } } I…
anticafe
  • 6,816
  • 9
  • 43
  • 74
2
votes
1 answer

Is it possible to spy on suspend Android Room DAO functions with MockK

I am investigation the MockK library with my Android JUnit tests testImplementation "io.mockk:mockk:1.10.0" I have an issue when attempting to spyk on suspend functions heres my Junit…
Hector
  • 4,016
  • 21
  • 112
  • 211
2
votes
1 answer

Kotest parallelism with Mockk

So I've been using kotest in combination with mockk, but I encountered an issue when switching from single thread mode to multiple by adding this to ProjectConfig class: override fun parallelism(): Int =…
SMGhost
  • 3,867
  • 6
  • 38
  • 68
2
votes
0 answers

MockK: capture() intercepts calls that do not match the full list of parameters in the enclosing verify()

In the below example I would expect capture() to engage only when the full list of parameters to verify() matches, however the opposite seems to be true. Is this behavior intended? interface ExternalService { fun doIt(doCapture: Boolean,…
John-Arne Boge
  • 153
  • 1
  • 6
2
votes
0 answers

Mockk - mocking javax.mail.Message - getting java.lang.VerifyError

I want to mock instance of javax.mail.Message, but unable to. Getting java.lang.VerifyError with no message. Running: Kotlin - 1.3.10 Mockk - 1.9 Simple code: import io.mockk.every import io.mockk.mockk import org.junit.Test import…
2
votes
0 answers

io.mockk.MockKException: no answer found for whereas any() is used as an argument

Currently, I'm working on unit tests and I'm using Mockk for that purpose. Here's how my unit test looks like (I'll put the most important lines) @Test fun `Test computeDowntimesOverPeriod with starting uptime`() { every…
Dat Nguyen
  • 232
  • 2
  • 15
2
votes
0 answers

How to do coVerifyOrder of livedata.postValue(any()) - It is returning io.mockk.MockKException: Failed matching mocking signature for

Scenario - Hi I am new in testing using mockk. I want to test the order in which methods are being called in viewmodel. And I want to test livedata.postValue in verify block{} but mockk is giving an exception. Kindly also help me to understand the…
Rahul Lohra
  • 824
  • 1
  • 10
  • 21
2
votes
0 answers

MockK System.getenv only

Language: Kotlin The goal is to test a method that calls System.getenv internally, I'd like to make this method produce a predefined result Tried: mockkStatic(System::class) every { System.getenv(any()) } answers { when { …
echen
  • 2,002
  • 1
  • 24
  • 38
2
votes
0 answers

Mockk. How to test one method and skip initializing everything in the class

I want to test the method-parsing and skip all the business logic. In the project I use the library Mockk. I tried a lot of everything from the documentation, but didn’t take a look at complex implementations. The expected result was not. I used…
viad899
  • 43
  • 5
2
votes
1 answer

kotlin : unit test with mock injection (mockK)

I followed step by step instructions from many blogs for implementing a mock with MockK: class SWServiceImplTest { @MockK lateinit var externalApi: ExternalApiService @InjectMockKs lateinit var SWService: SWServiceImpl …
johann
  • 1,115
  • 8
  • 34
  • 60
2
votes
0 answers

Mockk - MockKException when testing password against regex

I've just started to do Unit testing in Kotlin using Mockk. I'm trying to test the following function: fun evaluatePredicate(regEx: String, passwordInserted: String) : Boolean { return passwordInserted.matches(regEx.toRegex()) } My…
makles
  • 55
  • 1
  • 7
2
votes
2 answers

Kotlin: how to clean up or reset mocks with JUnit5 and Mockk

In some case it's needed to clean up or reset the mocks between tests cases. Using Kotling with JUnit5 and Mockk, a first approach should be like this: class CreateProductsTests { @Test fun `run() with an existing product should throw a…
p3quod
  • 1,449
  • 3
  • 13
  • 15
2
votes
1 answer

Kotlin-mockk chain call

I am trying to perform a chain call within my unit test using the mockk library within Kotlin. Below is my code: @MockK lateinit var crypto: Crypto @Before fun setUp() { MockKAnnotations.init(this, relaxUnitFun = true) } @Test fun…
rj2700
  • 1,770
  • 6
  • 28
  • 55