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
2 answers

MockK: mocking an jpa repository.save call

My code saves an object to database in some bigger method, but I don't need to test this. So I want to mock the Repository.save method. But the save method returns the saved object. I tried the following: @MockK private lateinit var…
Meier
  • 3,858
  • 1
  • 17
  • 46
0
votes
2 answers

How to mock a constructor with a spy?

I am currently trying to test my Exposed Kotlin code. I have a table that follows the form object Foo: Table() { *parameters* } and a method that looks something like fun addNewFoo(){ Foo.insert { ... } } I'm testing addNewFoo and I want to…
jpthesolver2
  • 1,107
  • 1
  • 12
  • 22
0
votes
1 answer

How to test jdbctemplate insert using MockK?

I am trying to implement the same given below using MockK instead of Mockito. I am not able to mock the lambda function inside the JDBC template update() method. class Repository(private val jdbcTemplate: JdbcTemplate) { createRule(emp: Employee):…
0
votes
1 answer

Mocking Android Snackbar for Mockk Unit Testing

I want to to mock the Snackbar class, but the initialization code below keeps throwing errors. @MockK lateinit var snackbar: Snackbar It looks like the mock initalizer has to access BaseTransientBottomBar's static method, which calls…
ssudev
  • 1
  • 3
0
votes
1 answer

How do I test a Kotlin suspend call in Android with MockK?

I'm trying my hand at TDD with an Android app. I'm writing it in Kotlin, and because of that I've turned to MockK for testing, but there's one thing (for now) that I haven't been able to find out how to do: test a suspend call. I wrote a test for a…
Arreis
  • 119
  • 2
  • 12
0
votes
1 answer

MockK - cannot mock same function twice

I am trying to test the getTopicNames function (below) in two scenarios: If it succeeds and if it does not succeed. fun getTopicNames(): Either> = try { adminClient.listTopics() .names() …
LeYAUable
  • 1,613
  • 2
  • 15
  • 30
0
votes
2 answers

declareMock<> not working with mockk in android unit test

i am trying to mock a repository class using declareMock and mockk but it doesn't seem to be working as i am getting the data from the real repository. Version implementation "org.koin:koin-androidx-viewmodel:2.1.6" testImplementation…
or_dvir
  • 479
  • 1
  • 7
  • 22
0
votes
1 answer

Android UI testing: Why LiveData's observers are not being called?

I have been trying, without success, to do some UI tests on Android. My app follows the MVVM architecture and uses Koin for DI. I followed this tutorial to properly set up a UI test for a Fragment with Koin, MockK and Kakao. I created the custom…
0
votes
1 answer

mockK cannot differentiate types in every statements

I am writing a method in Kotlin which returns elasticsearch indices that have an alias assigned to them: fun getActiveIndices(cluster: ElasticsearchCluster): List { val aliases =…
Enes Keles
  • 141
  • 1
  • 15
0
votes
1 answer

Verification failed - function is not called

I'm trying to test a function like this: override suspend fun create(item: Location): FlowResult { val (base, version) = locationRepositoryMapper.map(item) return locationRemoteSource.create(base, version) .flatMapConcat { …
P. Savrov
  • 1,064
  • 4
  • 17
  • 29
0
votes
0 answers

How to use Mockk to mock a dependency in tested class without passing it in as an argument?

If I want to test a method that uses some 3rd party library, and I want to mock that library, the documentation seems to suggest that I need to pass in a mocked instance of a library class. But what if the class imports its own dependencies, like…
TLP
  • 1,262
  • 1
  • 8
  • 20
0
votes
1 answer

writing unit test cases in kotlin using junit5 AND mockk

i am new to kotlin, junit5 and mockk. i am writing unit test cases for a function which belongs to companion object of a class. how to write unit test cases for this. class CommonUtility { companion object { …
Akash Patel
  • 189
  • 1
  • 5
  • 13
0
votes
1 answer

How does the Mockk library mock a non-interface class?

When I built a structure like the one below, I saw that the classes that are not executed through the interfacecan also be mockable. How does this work? Could it be related to Kotlin? In fact, my question here is; How does it crush the function of a…
Hasan Kucuk
  • 2,433
  • 6
  • 19
  • 41
0
votes
1 answer

Error in POST method with Kotlin, SpringBoot and Mockk

I have a problem with a test (mock) of type POST in kotlin, when i use a data class with a date field (LocalDate). This is the Stack im using: springBoot : v2.1.7.RELEASE Java : jdk-11.0.4 kotlinVersion : '1.3.70' junitVersion :…
Fernando
  • 7
  • 3
0
votes
1 answer

test case using mockk in not working for function

I am new to mockk framework and trying to write a test case for below function fun fromCityWeather(cityWeather: List): HomeWeather { return HomeWeather( cityWeather.first().temperature, …