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

Mock generic top level suspend function with mockk

I have a function inside a class that is using a generic top level suspend function. In order to test this function I would need to mock this top level function, but so far I have not found a nice solution. Lets say I have this: suspend fun
rimes
  • 761
  • 1
  • 8
  • 25
3
votes
2 answers

Unresolved reference: mockk

I have imported mockk library in commonTest -> shared module. There are no import errors in the test classes, but when I run the test I get errors like: Unresolved reference: every Unresolved reference: mockk Unresolved reference: verify in all…
Sunbey13
  • 339
  • 4
  • 12
3
votes
1 answer

Mockk: Mock a global function with overloads

Mockk added support for mocking global functions. However, when they have overloads there's a problem. For example if you try to mock delay() with mockkStatic(::delay) you encounter this error: Overload resolution ambiguity: public suspend fun…
mmm111mmm
  • 3,607
  • 4
  • 27
  • 44
3
votes
2 answers

Not able to use MockK in Android Espresso UI Testing

I am getting a error when trying to use MockK in UI test which was perfectly working in Unittest cases MockK could not self-attach a jvmti agent to the current VM Full error report Caused by: io.mockk.proxy.MockKAgentException: MockK could not…
RockyGlobal
  • 525
  • 5
  • 13
3
votes
1 answer

Mockk library CoroutineScope ClassCastException

I am trying to mock this function below by using Mockk library. fun launchOn(block: suspend CoroutineScope.() -> Unit) { viewModelScope.launch(Dispatchers.IO) { block() } } I tried to mock following below private val viewModel:…
ycannot
  • 1,807
  • 1
  • 9
  • 20
3
votes
1 answer

class java.lang.Object cannot be cast to class Task when mocking Spring Repository with MockK

I have a pretty simple Spring repository unit test for my Task entity. Here is how the repository is configured: @Repository interface TaskRepository : CrudRepository Here is the logic I'd like to test: // TaskService.kt fun…
xetra11
  • 7,671
  • 14
  • 84
  • 159
3
votes
2 answers

Mockk match overloaded function with generics

So I have 2 overloaded score functions, one of which takes a performance param of type Performance, and the other which takes a performances param, which is a List. The former returns a double, while the latter returns a double array.…
1419636215
  • 275
  • 2
  • 13
3
votes
2 answers

mockk, clearAllMocks or unmockkAll

mockk 1.9.3 In the test noticed if did static mock in previous test, the next test will be using same mock. Thought to do a reset at @After, but not sure which one to use clearAllMocks or unmockkAll. in https://mockk.io/ unmockkAll unmocks object,…
lannyf
  • 9,865
  • 12
  • 70
  • 152
3
votes
0 answers

How to Mock Kotlin LifecycleCoroutineScope for Unit Test?

How can I mock the following function act. class Some { internal lateinit var coroutineScope: LifecycleCoroutineScope ... fun act() { coroutineScope.launch { ... } } } I am using mockk and…
3
votes
1 answer

Why my org.springframework.test.web.servlet.MockMvc framework is failing to resolve constructor parameters of a controller

I want to create a unit tests for my Spring Boot app, but I receive following error during launching one of the UserControllerTest test: *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0…
rojarand
  • 445
  • 6
  • 18
3
votes
1 answer

JUnit 5: How do I use multiple extensions in a test written in Kotlin?

I am trying to migrate my tests for a ViewModel from JUnit 4 to JUnit 5, and use MockK in conjunction. In JUnit4, I have made use of rules--namely rules for RxJava2, LiveData, and Coroutines within one test, and it has worked well. Here's how I use…
Alvin Dizon
  • 1,855
  • 14
  • 34
3
votes
0 answers

Mock method to throw PersistenceException and StatusPages receives a different exception

I was trying to make a unit test that verifies that an exception thrown by hibernate would be handled and would result in a proper response. The exception is a PersistenceException() with cause being a ConstraintViolationException(). So I handled it…
3
votes
3 answers

MockK causing a StackOverflowException

As title! I can't run any tests that are using MockK, as they end up throwing a StackOverflowError during initialisation. As an example, here's one of my test classes: @ExtendWith(MockKExtension::class) class DBSequencesServicesTest { private…
Ian Knight
  • 2,356
  • 2
  • 18
  • 22
3
votes
1 answer

Does Mockk support Kotlin 1.4.10

I have upgraded my Android Application to kotlin 1.4.10 Now my tests are failing when trying spyk() classes with this exception io.mockk.MockKException: Can't instantiate proxy for class com.aaa.bbb.ccc.MyClass with a strange class cast exception…
Hector
  • 4,016
  • 21
  • 112
  • 211
3
votes
2 answers

How to use spyk

I need to verify that a method has been called on an object. So I make a spy of this object: obj = spyk(obj) And then I verify that a method has been called: verify(exactly = 1) { obj.f(3) } The test fails with the following error…
Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69