📜  mockito kotlin inorder - Kotlin (1)

📅  最后修改于: 2023-12-03 15:32:55.201000             🧑  作者: Mango

Mockito Kotlin InOrder

Mockito is a popular mocking framework for unit testing in Java. With the release of Mockito 2.1.0, Kotlin is now officially supported. Mockito's InOrder feature allows the verification of interactions in a specific order.

Setting up Mockito

Before we can use Mockito in our Kotlin project, we first need to add it as a dependency in our build.gradle file:

dependencies {
    testImplementation("org.mockito:mockito-core:3.13.0")
}

After adding the dependency, we can start using Mockito in our tests.

Creating a mock object

Mockito allows us to create mock objects of interfaces or classes. In Kotlin, we can create a mock object using the mock function:

interface MyInterface {
    fun foo()
}

val mockObject = mock<MyInterface>()
Creating an InOrder object

To verify interactions in a specific order, we need to create an InOrder object:

val inOrder = inOrder(mockObject)
Verifying interactions in order

We can now use the verify method on the InOrder object to verify interactions in the expected order:

mockObject.foo()
mockObject.bar()

inOrder.verify(mockObject).foo()
inOrder.verify(mockObject).bar()

This code verifies that foo was called before bar.

Conclusion

Mockito's InOrder feature is a useful tool for verifying interactions in a specific order. With Mockito now officially supporting Kotlin, it's easier than ever to use it in our Kotlin projects.

References