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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
5 views

Retrieving latest value of local test property with Mockk

My understanding of coEvery and every in the mockk library is that every time the method referenced inside the lamda was called, it would return the value specified. However I'm seeing the value to be ...
Kyy's user avatar
  • 1
3 votes
0 answers
33 views

What is the purpose of having coroutine-specific functions?

We can't use every and verify with coroutines because they don't take suspend functions as parameters. We have to use coEvery and coVerify instead. But why can't we use coEvery and coVerify with non-...
k314159's user avatar
  • 6,299
1 vote
0 answers
17 views

MockK throws ExceptionInInitializerError when running Android Tests on Emulator from Gradle

I've written some Android Tests (instrumented) for our Kotlin + Compose project. They heavily rely on MockK for mocking/stubbing. I'm trying to run the tests from the CLI (using Gradle), so I can hook ...
Elias's user avatar
  • 13
2 votes
1 answer
43 views

Kotlin: Type mismatch: inferred type is Runs but Awaits was expected

I am currently writing tests using kotest and MockK in Kotlin. I encountered an error during the mocking process while writing tests in Kotlin using kotest and MockK. every { accountService....
iknow kim's user avatar
-1 votes
0 answers
18 views

Android unit testing with mockk that receives flow

I have a class that talks to a use case and gets a flow back Flow<BaseResponse> I want to unit test my methed deleteDigitalKey I am new to using the flow. Need help pls. Here is the code ...
vincent's user avatar
0 votes
0 answers
23 views

Mockk, Kotest - How to test reactive subscribe onError callback?

I have this simple function calling http request override fun refreshDocument( investmentIntermediary: InvestmentIntermediary, receivedDocumentUuid: String ) { if (...
jhrmdk's user avatar
  • 603
-1 votes
0 answers
17 views

Using Mockk capture function when the parameters are updated

I am using Mockk to write some tests I have a function that takes in an object, and within that object I want to make sure that one specific field has the correct value. Below is a basic example of ...
Santi's user avatar
  • 379
0 votes
1 answer
28 views

Jetpack Datastore: how can I test that my IOException handling is working correctly?

WE have a solution in which we have a few data points that are stored in a Perferences Datastore. We realised that we had failed to account for the IOException correctly, and made a fix. For peace of ...
Timo Newton's user avatar
0 votes
1 answer
19 views

Mocking a gradle project in MockK doesn't call the stubbed method

I am writing a unit test in kotlin using JUnit and MockK to test my custom Gradle task. open class CopySourceTask @Inject constructor( private val config: MyCustomExtension ) : DefaultTask() { ...
newbie's user avatar
  • 1
1 vote
3 answers
162 views

How do I mock a File.copyTo in Mockito and Kotlin

Simply put, I've got a File object that product code will invoke the copyTo method on. Similarly, I'm looking for an equivalent mocking technique for File.inputStream In the unit test, I just want a ...
selbie's user avatar
  • 101k
0 votes
1 answer
46 views

Mockk test is failing after upgrading gradle and kotlin

I have an Android Studio project that hadn't been updated for a while. I upgraded from gradle 4.x to 8.x and from Kotlin 1.4 to 1.8. After doing so, the following test fails and I can't figure out why....
MSohm's user avatar
  • 817
0 votes
1 answer
45 views

How to mock Java static block

I have a following Java class in a 3rd party library: public final class SomeFactory { public static native someMethod1() public static native someMethod2() private SomeFactory() { } ...
Pitel's user avatar
  • 5,216
0 votes
0 answers
18 views

How to test a method, what combines RxJava and Suspend fun?

So I have a simple test, where I try to test the error result, something like this: @Test fun `test`() { every { Util.fetchLocation()} returns Single.error(Exception()) val ...
Andreas1234's user avatar
0 votes
1 answer
117 views

How to capture arguments with io.mockk in unit-test

I am having difficulties when trying to use a CapturingSlot<*> from mockk. I get the error message shown in the above image See the paragraph named Capturing in https://mockk.io As explained in ...
jactor-rises's user avatar
  • 2,565
1 vote
2 answers
148 views

Tell MockK which objects to use for injection

Is there a way to tell MockK which object to use when injecting dependencies? Here's a simplification of what I want to do. interface InternalDependency { abstract funToMock() } class Target() { ...
Keverly's user avatar
  • 460
0 votes
0 answers
7 views

ObserverField<Drawable> unit test in Android kotlin

How can we write unit test case for view model If we have ObserverField Drawable As variable It is mostly showing Absent Code attribute in method that is not native or abstract Or we can't write unit ...
Snehal Gongle's user avatar
0 votes
1 answer
100 views

Mocking Static Methods - Kotlin

I've been using Mockito, Mockk and PowerMock to mock static methods, to not avail. I'm not sure if my understanding is correct, but I was expecting that, when I mock a (static or instance) method, the ...
Elias's user avatar
  • 13
0 votes
1 answer
124 views

Error Cannot invoke "String.isEmpty()" because "parent.path" is null for mockk kotlin datastore functions

I am writing a Test class using mockk framework of koltin for data store. The class looks like `open class MyStorageClass(context: Context) : Storage { private val testDataStore = context....
Satyam Gondhale's user avatar
0 votes
0 answers
24 views

how to make correct test

I try to make test of this with mockk but I can't make it because of Coroutine. notesUseCases.getAllNotesUseCase.invoke() returns Flow<List<Note>>, and note is something like this Note(1, &...
Artur Andruszkiewicz's user avatar
0 votes
1 answer
41 views

how to see why mockk assertEquals(...) fails

so i have a test like this: verify { function.send( withArg { assertEquals(expected, actual) }, withArg { assertEquals(expected, actual) }) } and i'm ...
ffff's user avatar
  • 1
0 votes
0 answers
78 views

Why is mockkConstructor() not working on the every block?

I'm trying to mock a constructor of a class that is a lazy variable of the class I'm testing: private val manager by lazy { manager(Data(ID_ADMIN, MANAGER_CODE), context) } I have the mockk() ...
Dave_August's user avatar
0 votes
0 answers
31 views

Problem with testing a method in ViewModel

I have a method in a ViewModel that I need to test. I use Mockk library for testing and in this test, I need to make sure that useCase methods are called in a ViewModel. This is a method in ViewModel: ...
Alex20280's user avatar
1 vote
0 answers
79 views

Junit5 concurrent execution mode on a class impacting another test class?

I have a Kotlin project, using JDK 17, Junit 5.10.0, and junit-platform 1.10.0 (the latest Junit versions as of today) with several thousands of unit tests. Because of some framework choices we made, ...
Vincent F's user avatar
  • 6,733
0 votes
1 answer
53 views

IndexOutOfBoundsExpection when testing asynchronous functions in Kotlin with mockk

When testing a function I have problems with an IndexOutOfBoundsException. Normally, usersInRangeViewStates should have size two, with the first entry Resource.Loading and the second Resource.Success. ...
Kleini's user avatar
  • 57
0 votes
0 answers
121 views

Kotlin unit tests fails on github but pass locally

class HomeViewModelTest { private var parkingService: ParkingService = mockk() private lateinit var viewModel: HomeViewModel private lateinit var loadingState: MutableList<LoadingState&...
user22336635's user avatar
1 vote
1 answer
66 views

mockk, why it cannot stub the PackageInfo.versionName

Tryin to mock the android.content.pm.PackageInfo, and stub the versionName. @Test fun test_() { val pInfoMock = mockk<PackageInfo>() every { pInfoMock.versionName } ...
lannyf's user avatar
  • 10.1k
0 votes
0 answers
58 views

mockk, How to use mockk to stub a base class protected function on an instance of derived class

Having base class: abstract class BaseNotification<T: Any> { protected abstract fun convertToJson(message: T): JSONObject? companion object { @JvmStatic @Throws(...
lannyf's user avatar
  • 10.1k
0 votes
0 answers
393 views

mockkObject calling real method instead of mocked object class | Android | Kotlin | Unit test

I am using mockk for unit testing my Android project. I am having an object class and that object class is used inside the repository which I am testing. The mockk library I am using : "io.mockk:...
raj abimanyu's user avatar
0 votes
1 answer
15 views

mockk, which one to use for calling a un stubbed function on the mock

having a class: class ClassType { fun func(v: Int) : Int{ return v * 2 } } if it has mocked the class: val mock = mockk<ClassType>() and does not want to change the func(...
lannyf's user avatar
  • 10.1k
0 votes
0 answers
95 views

mockk function with Flow inconsistence

I am writing an Android Unit Test for a ViewModel. I am mocking a flow like this: coEvery { showRepository.currentShow() } returns flowOf( ShowWithRelations( localShow.show, localShow....
Luca Nicoletti's user avatar
0 votes
1 answer
49 views

Unit Testing ViewModel - Data in returned object is always null

This is my ViewModel @HiltViewModel class MovieDetailsViewModel @Inject constructor( private val repository: MovieRepository, private val savedStateHandle: SavedStateHandle, private val ...
greysqrl's user avatar
  • 947
0 votes
0 answers
111 views

How to test using mockk library kotlin for Handler class?

Code: public class MyApplication extends Application { private Handler handler; private Context context; @Override public void onCreate() { super.onCreate(); context = ...
Magic Man's user avatar
0 votes
0 answers
58 views

Controller Unit Test for File Upload Feature

I am currently working on implementing a file upload route in our Spring Boot application, Here is a brief overview of the feature: It accepts a multipart file through a POST request to the "/api/...
Hadi Rifaii's user avatar
0 votes
0 answers
141 views

mockk, spyk(map) is failing

android project, having some unit test working fine until updated to java 17. Here is a simplified test to show that the spyk(HashMap()) gets error @Test fun test_() { val metaMapSpy: ...
lannyf's user avatar
  • 10.1k
1 vote
0 answers
64 views

Cannot mock Android drawable using @Mockk annotation?

I want to mock an Android drawable for some unit tests. I'm using the @Mockk annotations. This is the code: @MockK lateinit var mockIcon: Drawable @Before fun setUp() { MockKAnnotations.init(this)...
Toni Joe's user avatar
  • 7,837
0 votes
0 answers
124 views

Spring boot service in command line runner with @MockkBean cannot be mocked

I have created a small reproducer for an error that I get while I try to test a command line runner application built with Spring boot. I have a simple application with one service @...
geoandri's user avatar
  • 2,368
2 votes
1 answer
46 views

Mockk: Stubbing same function twice ignores first behaviour

I want to provide a null value for all calls with an id >= 100 -- and a concrete Product instance with the captured id for all others: every { repoMock.findById(more(100, true)) } returns null ...
Tom's user avatar
  • 2,015
0 votes
1 answer
80 views

Test suspendCoroutine with Mockk library

I had a function and a unit test which worked but when I added a suspendCoroutine - a unit test starts to fail. Here a simplified versions of them: fun loginOld() { logInUseCase.execute( ...
Alexey Simchenko's user avatar
0 votes
0 answers
59 views

AbstractMethodError when Mocking MultipartFile in Spring Boot Test

I'm trying to mock a MultipartFile in a Spring Boot test using the Mockk library. I'm experiencing an issue where I get an AbstractMethodError when calling the getContentType() method on the mocked ...
QendresaZ's user avatar
1 vote
0 answers
79 views

Fail to mock jOOQ 3.18 classes with MockK, works with 3.14

I work on a code basis written in Kotlin that uses jOOQ to interact with databases. It uses jOOQ to generate the "meta model" i.e. classes abstracting tables, fields, etc. jOOQ generates ...
Marcel Stör's user avatar
  • 22.8k
-1 votes
1 answer
489 views

io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock In Android Kotlin

Mockk Error in android Koltin Unit test. matching with Two URL io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock @Before fun ...
Android Dev's user avatar
0 votes
1 answer
129 views

How to match anonymous object in mockk

I am trying to verify a logger::error method called like this: logger.error(MY_ENUM_VALUE, object { val field0 = field0Value val field1 = field1Value }) I wonder how to match the anonymous ...
EnzoMolion's user avatar
0 votes
1 answer
55 views

Matching Error (assertEquals) when unit testing ViewModel with Hilt and Mockk

I am trying to unit test my ViewModel. My code is written using Kotlin, Android-X and Jetpack compose and I am using Hilt for Dependency Injection and Mockk for... Mocking The issue I'm having is that ...
greysqrl's user avatar
  • 947
0 votes
1 answer
67 views

JUnit testing for Firebase Realtime Database DataSnapshot with mockk

I try to write tests for async functions that load data from the Firebase Realtime Database. suspend fun getUsers(): Resource<GETUsers> = try { val users = databaseReference.get()...
Kleini's user avatar
  • 57
0 votes
0 answers
49 views

The Mockk library does not redefine the class in runTime

There is an application that connects to RabbitMQ at startup, all I'm trying to do is implement a test and at the same time not access RabbitMQ from the test. For some reason, my changes in the test ...
user5285766's user avatar
0 votes
1 answer
92 views

Testing private functions with junit and mockk in Kotlin

How do I test a private function called in the init block using junit and mockk in Kotlin? Example: class viewModel(private val repository: Repository) : ViewModel() { init { doSomething() }...
Kleini's user avatar
  • 57
1 vote
1 answer
65 views

MockK cannot resolve Generic T from method from abstract class extended

All my services extend one abstract class DefaultBaseService<T :HasID<ID>, ID> with some basic CRUD methods like: override suspend fun update(id: ID, obj: T): T { obj.id = id ...
Alice's user avatar
  • 11
1 vote
0 answers
87 views

Controller test is failing due to MockkException as no answer is found for the mocked service method

I'm testing my controller containing two endpoints, one for signup and another for signin. Signup is the one failing while signin succeeds. Here's the stacktrace Request processing failed: io.mockk....
Menyten's user avatar
  • 39
1 vote
1 answer
701 views

How to test coroutines with await() in Kotlin?

I'm trying to create tests with mockk for my suspend functions that include await(). I have tried the following code so far, but the test never ends. private lateinit var repository: AuthRepository ...
Kleini's user avatar
  • 57
0 votes
1 answer
85 views

Debugging and the actual test result differs when mocking a object in Kotlin

I have written a testcase in which I mock a object using mockk. The problem is that when I run the testcase and debug using Intellij I do get the mocked response inside the debug window but when I let ...
MrAndre's user avatar
  • 861

1
2 3 4 5
11