Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 mockito

I saw this question before Equivalent of doReturn(x).when(y)... in mockk?

But the answer is: every (method) returns (value)... which is not what I want. Because every(method) will actually run the method

like image 449
dosu Avatar asked Feb 21 '26 02:02

dosu


1 Answers

With “every” not executes the original method if you not wish, “every” invalidate the call to original and returns the data declared for will returned.

Example:

every { yourMock.getData() } returns yourData

If you want call original method can use:

every { yourMock.getData() } answers { callOriginal() }
like image 111
Igor Román Avatar answered Feb 27 '26 08:02

Igor Román



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!