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

How can I mock a call to Spring's repository `saveAll()` method using mockk?

I am using Mockk as my mocking framework when testing my Spring Boot Data repository interfaces. Actually I am doing the following every { itemRepository.saveAll(listOf(any(), any())) } returns listOf(mockk()) which should mock the following…
xetra11
  • 7,671
  • 14
  • 84
  • 159
1
vote
0 answers

How can I mock callback pass in constructor

I have below class that has callback.How Can I mockk ResponseReceiver(callback) class Receiver(private val context: Context) { val responses: Flow = callbackFlow { val callback = object : Server { override fun…
Pause
  • 33
  • 9
1
vote
0 answers

coVerify fails when running multiple tests but succeeds if the test is run alone

I am using open weather map to get weather data. For this I call the api like this: openWeatherMapAPI.oneCall().historical() .byCoordinateAndTimestamp( Coordinate.of(latitude.toDouble(),…
1
vote
1 answer

Mockk verify wasNot called failed

I have a question regarding mockk. When using MockK's wasNot called to check that ClassA is called, there is a problem that the test fails if it is called in another test even if ClassA is not called in the current test. Why is this happening and…
eleven22
  • 83
  • 2
  • 7
1
vote
1 answer

How to mock a field in object in kotlin?

I have a class object Sender { private val emailSender: EmailSender = EmailSender() fun sendMessage(text: String) { val message = Message(text) message.createHeader() emailSender.send(message) } } I need to…
Violetta
  • 509
  • 4
  • 12
1
vote
1 answer

Convert non-nullable to nullable in Kotlin

I have an Entity class as below: @Entity(name = "Person") @Table(name = "person") class Person( _firstName: String? = null, _lastName: String?, _address: String? = null) ) { @Id @GeneratedValue(strategy = GenerationType.AUTO,…
newLearner
  • 637
  • 1
  • 12
  • 20
1
vote
1 answer

MockK test member function getting NullPointerException

The class under test looks like: class State(pivate val repo){ val values = listOf() fun update() { values = repo.generateValues() // <-line 375 } } The unit test looks…
r0n9
  • 2,505
  • 1
  • 29
  • 43
1
vote
1 answer

How mockk is able to return object wrapped in Result when returnArguments

I am still learning kotlin, and I am curious how is it possible that mockk is able to return some object T wrapped in Result. For better understanding let analyse below example: We have such method definition fun save(toSave : Entity):…
Kacper
  • 451
  • 6
  • 17
1
vote
1 answer

Mock boolean property using Mockk is not working

I'm using Mockk and I want to test a MediatorLiveData that depends of some boolean properties of the class. I was using mockkConstructor(Boolean::class) but suddenly a warning appears on the console log and all the test cases fails (I'm not sure but…
1
vote
0 answers

java.lang.InstantiationError when mocking method with interface as generic type in Kotlin using Mockk

I am using Kotlin 1.7.10 and Mockk 1.12.4 and I have a factory method in the following spring bean class: @Service class DataSynchronizationManagerFactoryService( private val platformTransactionManager: PlatformTransactionManager ) { fun
Andy
  • 1,127
  • 2
  • 12
  • 25
1
vote
2 answers

How do I set a default Answer strategy with Mockk

In Mockito, I can create a mock of a class, and specify the default answer for any function calls to that mock, like this: whenever(this.strings).thenReturn(mock(StringProvider::class.java, StringProviderAnswer())) Where the answer might be …
M Dapp
  • 1,453
  • 16
  • 31
1
vote
1 answer

Unit Test kotlin for hibernate.sessionfactory.createQuery using mockk, what does this mean?

I'm trying to mock out my calls to hibernate using the mockk framework. I need to mock the Query object that is returned by here. When I use the following code I get this compiler error, which not being a kotlin developer I don't understand. Type…
XenoPuTtSs
  • 1,254
  • 1
  • 11
  • 31
1
vote
1 answer

Mock all methods except one in JUnit test with Mockk llibrary

How to mock or spy some methods but use real logic of another method in the same class using Mockk library? Here is a simplified logic of parent-child classes that I have: open class Parent() { var condition = true fun method1() { if…
Alexey Simchenko
  • 333
  • 3
  • 15
1
vote
1 answer

Mockk call lambda when other method is called

I'm trying to create this MockController with mockk to avoid create a new class for testing. Is possible to do that? class MockController : IController { override lateinit var output: (String) -> Unit override fun start() { …
1
vote
1 answer

Mockk verify is trying to verify Log.d() which is never been called

So, I have written unit test as follows. Which basically calls a method in Viewmodel class. @Test fun `on clear Cached Calls AppUtility ClearCache`() { sut.clearCache() verify(exactly = 1) { …
AkshayT
  • 2,901
  • 2
  • 28
  • 32