casrecipe.blogg.se

Foreach kotlin index
Foreach kotlin index











foreach kotlin index

Regardless the way it was used to capture the argument, it stores the last captured values and can be accessed like: slot.captured Compatibility Problems We can use the slot which can also be used in both situations:ġst When mocking the function to get the argument that was passed when the function was invoked: val slot = slot() whenever(genericClass.func(capture(slot))).thenReturn(stubResponse)Ģnd When verifying if a function was called, also to get the argument that was passed when the function was invoked: val slot = slot() verify(genericClass).func(capture(slot))) (where x is the index of the wanted value given the order it was captured) Regardless the way it was used to capture the argument, it stores the captured values in a list, and can be accessed like: captor.firstValue We can use the argumentCaptor which can be used in both situations:ġst When mocking the function to get the argument that was passed when the function was invoked: val captor = argumentCaptor() whenever(genericClass.func(captor.capture())).thenReturn(stubResponse)Ģnd When verifying if a function was called, also to get the argument that was passed when the function was invoked: val captor = argumentCaptor() verify(genericClass).func(captor.capture()) Given we have the mock of a class called genericClass that has the function func, with only one parameter of type GenericType that we want it to return a value called stubResponse.

foreach kotlin index

Verifying arguments from called functions Ps.: For cases when you want to verify if the function was called with a specific parameter it works the same way for both libraries, so the examples have been built with only one parameter using the any() generic function (which in case of Mockk will import the function from the Mockk library).

foreach kotlin index

We can have tests using the annotation to define our mocks, because of that we have to add the deprecated MockitoAnnotations.initMocks(this) to the function: private lateinit var mapper: GenericMapper fun beforeEach() Mocking attributesįirst lets tackle how we create our mocks by using Mockito. The aim of this document is to show how to use Mockk functionalities from the point of view of a Android developer that already uses Mockito by going through its most used features.įor each topic we will cover how it’s done in Mockito and how is the similar in Mockk.













Foreach kotlin index