mockito kotlin inorder
To use Mockito with Kotlin, you can use the inOrder
function to verify the order of method calls. Here's how you can achieve this:
- Import the necessary Mockito and Kotlin dependencies.
- Create a mocked object using Mockito.
- Invoke the methods on the mocked object in the expected order.
- Use the
inOrder
function to verify the order of method calls.
Here is an example of using Mockito's inOrder
in Kotlin:
import org.mockito.Mockito.`when`
import org.mockito.Mockito.inOrder
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.junit.Test
class ExampleTest {
@Test
fun testMethodOrder() {
// Create a mock object
val mockedList = mock(MutableList::class.java)
// Define the expected order of method calls
`when`(mockedList.add("one")).thenReturn(true)
`when`(mockedList.add("two")).thenReturn(true)
// Invoke the methods on the mock object
mockedList.add("one")
mockedList.add("two")
// Verify the order of method calls
val inOrder = inOrder(mockedList)
inOrder.verify(mockedList).add("one")
inOrder.verify(mockedList).add("two")
}
}
In this example, we create a mock object, define the expected order of method calls using when
, invoke the methods, and then use inOrder
to verify the order of method calls.