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.
403
questions
0
votes
0
answers
15
views
How to write Kotlin unit test to test a function internally calling other functions
I'm new Kotlin and facing issue while writing Kotlin Junit test, unable to go inside if condition!
override fun process(message: String) {
try {
logger.info("(invFileTransformer) ...
0
votes
1
answer
36
views
Get number of invocations
Does MockK provide a way of finding how many times a method has been invoked on a mock object?
I'm looking for something like Mockito.mockingDetails(mock).getInvocations(), but for MockK.
I can only ...
0
votes
1
answer
20
views
mockkStatic for extension method with generic parameter: "Not enough information to infer type variable T"
FYI: I'm currently using Mockk-1.12.4 and Kotlin-1.6
I've got an extension method that returns an object of type T:
fun <T> Entity.selectReferenceAsSingleObject(referenceName: String): T {
...
1
vote
1
answer
30
views
How is mockk's allAny() used
I can't find any documentation on allAny() that I can understand. The official documentation describes it as a "special matcher that uses any() instead of eq() for matchers that are provided as ...
1
vote
1
answer
23
views
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 ...
0
votes
0
answers
28
views
How to improve Kotest+Mockk Spring Weblux Handler unit tests performance?
I have the following project, still in development: https://github.com/TarekSaid/blotit, using Kotlin and Spring Webflux.
I was writing my unit tests with Spock (Groovy), but after some issues with ...
0
votes
0
answers
47
views
Execution failed for task ':app:testDebugUnitTest'. > No tests found for given includes: [com.*](filter.includeTestsMatching)
I'm trying to migrate a project from JUnit 4 to 5. The project has had a lot of migrations lately, including the swap from Groovy DSL to Kotlin DSL, Mockito to Mockk and from 'legacy' xml to JetPack ...
0
votes
0
answers
26
views
How to mock out lambda passed to .use on Closable?
Given:
suspend fun putS3Object(
folderPath: String,
emptyContent: ByteStream = ByteStream.fromBytes(ByteArray(0)),
): PutObjectResponse {
val request = PutObjectRequest ...
0
votes
1
answer
33
views
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() {
...
-1
votes
0
answers
55
views
class java.lang.Object cannot be cast to class okhttp3.Request Mockk
Code :
val request = postData.invoke(POST_ID)
when (val result = executeSync.perform(request)) {
......
}
Test :
@MockK
private lateinit var request: Request
...
0
votes
0
answers
44
views
How to mock extensions function in kotlin
I am using Mockk in my project. I am trying to mock my extension function but it cannot find my function. I tried some piece of code but it cannot find the extension function inside my test. Can ...
1
vote
1
answer
36
views
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) -&...
0
votes
1
answer
23
views
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(...
0
votes
0
answers
15
views
How test lambs expression pass to adapter constructor using slot
I have method pass two parameters data and listener for selection item from adapter then I want to test lambda expression for passing adapter contractor like this code
@JvmStatic
@BindingAdapter("...
0
votes
0
answers
39
views
How to mock internal val variableName:Interface=Class using mockk in Kotlin
I am using one val like internal val variableName:Interface=Class when I am writing the unit test for the class I am mocking the variable using mockk. While running the test getting the below error
...
0
votes
1
answer
50
views
Mock method call and return value without running the method (MockK)
I saw similar post: How to mock method call and return value without running the method?
I am wondering if there is similar method we can use in mockK?
something like: doReturn.when(mock).method in ...
0
votes
0
answers
21
views
How to mock MutableSharedFlow collect with specify event
I need to test my viewModel code like below, and can't get how to pass emit event into MutableSharedFlow
ViewModel
interactor.flowState()
.collect { state: State ->
// this code block ...
0
votes
1
answer
49
views
Android - How to replace Hilt's @Inject contructor annotated classes with a fake class in tests
I learned about @UnunstallModules, which is really nice. But what if I do not have a module and the class is just annotated with @Inject?
For example, I have a Authentication class
@Singleton
class ...
0
votes
0
answers
33
views
Verification failed: call 1 of 1: InfoRepository(infoRepository#29).insert(eq([infoObj]), eq([]), any())) was not called
// Tests Function In Test Class
@Test
fun checkInsertInfo() {
val infos: MutableList<Info> = mutableListOf()
val info = Info()
info.infoNumber = "1"
infos.add(info)
...
0
votes
0
answers
31
views
Mockk and Kotlin version compatibility matrix
As of today, the latest version of Mockk is v1.12.4
What are the supported versions of Kotlin for this?
Is there any version compatibility matrix for Mockk and Kotlin available?
0
votes
0
answers
75
views
Is there a way to mock the invocation of a secondary constructor of a Kotlin data class using mockk
From the documentation of mockk.io regarding the mocking capabilities of constructors I can see the following:
class MockCls(private val a: Int = 0) {
constructor(x: String) : this(x.toInt())
...
0
votes
0
answers
38
views
Test events were not received
I'm trying to unit test some of my viewModels on my modularized project, but in Android Library modules, I receive this Tests events were not received, even the auto generated examples don't work. On ...
0
votes
1
answer
41
views
Mock Bigquery function in kotlin unit test
I am develop an unit test, which involved (com.google.api.services.bigquery.Bigquery). I mock the object with mockk @SpyK. But the every{} block report error when unit test start. Detail as follow
the ...
0
votes
2
answers
137
views
Mockk: no answer found for a mocked JPA call with partial matching?
I'm trying to write a Junit test case for an update API. The API is using saveAll method of JPA to perform updates. The API is working fine, but when trying to mock the saveAll method it is giving me ...
0
votes
0
answers
31
views
KMM-MockK: Cannot mock the expect object class when I write the unit test case
As the title says, currently I'm having issues using mockk when writing unit tests in commonTest in my KMM project.
In my shared module, I created a useCase class that uses the expected object class ...
1
vote
0
answers
48
views
Mocking scope function in Kotlin
I am using following object creation methods in kotlin
class Resource(
referenceFactory: ReferenceFactory,
id: String,
name: String,
role: ...
0
votes
0
answers
117
views
How can I mock a nested @Composable in jetpack compose?
I'm attempting to run non-instrumented tests using jetpack compose and robolectric. I've had some success in basic testing, for the most part tests work like their instrumented counter parts, but can'...
0
votes
1
answer
135
views
Mocking internal function call in Kotlin
I am a complete beginner in terms of Kotlin and I am finding some issues while trying to test out a Ktor based application.
I have a file in my endpoints package localized at org.example.endpoints....
1
vote
1
answer
55
views
How to mock a class of a library used by the app during instrumented test?
I am working on an Android library and I am writing an instrumented test for it using UI Automator and Mockk.
The library has a class called InstallManager which will install stuff on the device. I ...
0
votes
1
answer
92
views
Mocking methods that call JNI with Mockk
I am trying to add unit tests to an existing Android app that has some JNI libraries (only for ARM). I am using Mockk and Junit4, and I have the following:
This static class is mine, and it calls the ...
0
votes
1
answer
75
views
Mock class used in a method Kotlin
I am using mockk Kotlin library. I have a service Service that has a private method calling a 3d party client
class Service {
fun foo() {
bar()
}
private fun bar() {
client = Client()
...
0
votes
1
answer
75
views
How to write a MockK unit test for the following code involving exceptions
I just started coding in Kotlin and I've never used MockK before. I want to know how to write a mockk test for the schedule() function and to test the RejectedExecutionException.
fun schedule() {
...
0
votes
0
answers
144
views
Jetpack compose UI tests mockking MutableState
I am trying to combine jetpack compose UI testing with mockk. But have some issues when the state changes it gives me an error:
io.mockk.MockKException: no answer found for: SignInViewState(child^2 of ...
0
votes
1
answer
116
views
Mockk matching and overloaded function withArg
Hello I am trying to find a way to match an overloaded function inside of the verify using withArg
The doc doesnt really point this out
every { getResponse.Ids } returns listOf(121212L)
assert(...
0
votes
1
answer
38
views
I am trying to convert a java unittest case mockito to Kotlin MockK in android
This is the verify block and was working on java
verify(mockTokenError) {}
And later tried to convert to kotlin
verify{mockTokenError()}
also tried
verify{mockTokenError}
none of them works please ...
2
votes
1
answer
166
views
MockK's "every" "returns null" not returning "null" but a NetworkInfo object with null and false values
In my unit test I am calling the following:
every { connectivityManager.activeNetworkInfo } returns null
but when I debug I get a NetworkInfo object filled with null and false values
networkInfo =...
2
votes
1
answer
970
views
How to check if a method was not invoked with mockk?
I need to check if a method was not invoked in my unit tests. This is an example test I did that checks if the method was invoked and it works perfectly fine:
@Test
fun ...
0
votes
1
answer
289
views
Skip a parameter in MockK unit test, Kotlin
I use MockK library for unit testing.
Tested method contains strings that don't have meaning for a result. I want to check other variables, but have to define a behaviour of strings because they are ...
0
votes
1
answer
50
views
Can't mock Path extension function
This test fails. I got the package-class string from the decompiled class.
@Test
fun `path does not exist`() {
mockkStatic("kotlin.io.path.PathsKt__PathUtilsKt")
val ...
0
votes
0
answers
151
views
mocck.spyk on a interface object failing
when spy on a interface object, it got
WARNING: Failed to transform class com/module/EvntHandleTest$test_evtListener$1$listener$1
java.lang.IncompatibleClassChangeError:
the class:
///
open class ...
2
votes
1
answer
174
views
Test with Kotlin Coroutines is randomly failing
Let us suppose we have a class member whose purpose is
to bring 2 objects (let's say object1 and object2) from two different places and then create the final
result merging these two object in another ...
0
votes
0
answers
515
views
Mockk and Kotlin: Type mismatch: inferred type is () -> List<Array<Any>> but (Mutable)List<Array<(out) Any!>!>! was expected
I'm writing unit tests in Kotlin with Mockk and I'm running into this error: Kotlin: Type mismatch: inferred type is () -> MutableList<Array<out Any>> but (Mutable)List<Array<(out)...
0
votes
1
answer
183
views
How to mock a ProducerScope from callbackFlow builder in Kotlin Flow?
I'd like to test a function where I use the scope of a callbackFlow builder. Assuming I have a function inside the flow builder like this:
fun items(): Flow<Items> = callbackFlow {
getItems(...
0
votes
0
answers
24
views
Mockk : Single.just not returning expected value
I am migrating my UT code from Mockito to Mockk.
I am using the below code to stub data
SUT = spyk(DataViewModel(mDataType, mDataId, isRequired))
every {
SUT.dataManager.getData(
...
0
votes
0
answers
44
views
android unit test, how to test the activity crated by context.startActivity(intent)
Having an activity which has some functions need to be coverage tested.
class HandlerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ) {
...
0
votes
0
answers
55
views
Mockk anonymous class
I'm trying to mock anonymous class created in testInstance Sample:
class SomeClass(val someValue:SomeType) :SomeAnotherClass(){
val anonymousClass = object : AnotherClass{
override ...
2
votes
1
answer
643
views
How to mock function without class in Kotlin?
fun add() {
return 4+1;
}
class Calculator {
fun MathUtils() {
// do something
// calls add() function
val x: Int = add()
// return something
return x ...
0
votes
0
answers
47
views
If there compatibility between RestAssured(with MockMvc) and MockK
I am wanting to create/run an Integration test to assess the processes from the controller all the way to the services, as well as all the business logic in-between. However, I don't want to make an ...
0
votes
0
answers
167
views
Mock Java interface anonymous implementation in Kotlin with Mockk
I have simple Java interface with one method, which is implemented as anonymous class in Kotlin code using the lambda syntax. I'm trying to mock the interface construction so that the method of the ...
0
votes
1
answer
391
views
Mock API calls with Retrofit + coroutines + Mockk
Later Edit
I ended up to have my api service methods suspended and refactor my code as suggested by @LordRaydenMK.
The reason for using the library ru.gildor.coroutines:kotlin-coroutines-retrofit it ...