Home > Software engineering >  Verifying method call with enum in Kotlin
Verifying method call with enum in Kotlin

Time:10-29

I'm trying to verify that a method is called with a given argument. That argument is a non-nullable enum type. So I get the exception eq(SomeEnum.foo) must not be null. Here is a sample what I'm trying to do:

enum class SomeEnum {
    foo, bar
}

open class MyClass {
    fun doSomething() {
        magic(SomeEnum.foo)
    }

    internal fun magic(whatever: SomeEnum) {}
}

@Test
fun mockitoBug() {
    val sut = spy(MyClass())
    sut.doSomething()
    verify(sut).magic(eq(SomeEnum.foo))
}

Capturing does not work too. What can I do or is that really a bug as I assume?

CodePudding user response:

Because Mockito was designed for Java, it doesn't play well with Kotlin's null checks. A good solution is to use the mockito-kotlin extensions library: https://github.com/mockito/mockito-kotlin

It includes Kotlin versions of the matchers that won't return null. Add a dependency on mockito-kotlin and just make sure to import the Kotlin versions instead of the Java ones.

  • Related