3

Having a kotlin singleton static method

internal object TestSingleton {
    @JvmStatic
    fun staticMethod1 (str: String) {
        println("+++ === +++ TestSingleton.staticMethod(), $str")
        staticMethod2 (str)
    }

    @JvmStatic
    fun staticMethod2 (str: String) {
        println("+++ === +++ TestSingleton.staticMethod2(), $str")
    }
}

In java test code:

    @Test
    public void test_staticMethod() {

        try (MockedStatic<TestSingleton> theMock = Mockito.mockStatic(TestSingleton.class, CALLS_REAL_METHODS)) {

            TestSingleton.staticMethod1("test");
            theMock.verify(() -> TestSingleton.staticMethod2(eq("test")), times(1));
        }
    }

it runs fine but convert to kotlin it does not compile:

    @Test
    open fun test_staticMethod() {
        Mockito.mockStatic(TestSingleton::class.java, Mockito.CALLS_REAL_METHODS).use { theMock ->
            staticMethod1("test")

            theMock.verify(() -> TestSingleton.staticMethod(any(Context.class), "test"), times(1))
            // or
            theMock.verify(times(1), () -> TestSingleton.staticMethod(any(Context.class)) )

        }
    }

enter image description here having mockito version testImplementation "org.mockito:mockito-inline:3.12.4".

How to test static method using mockito in kotlin? Not tried mockk yet since having a lot tests have been working with mockito. Not sure how simple with mockk in this case.

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • Out of interest why are you using JvmStatic ? Are you calling the code from Java or something ? Personally I'd avoid using it, which will make your code much easier to test. There's a good explanation about this is Phillip Hauer's blog on Kotlin testing best practices: https://phauer.com/2018/best-practices-unit-testing-kotlin/#avoid-static-and-reuse-the-test-class-instance – PaulNUK Mar 14 '22 at 21:36
  • it is still be called from java code (in the process to covert to kotlin, but too large and can only be done piece by piece) – lannyf Mar 14 '22 at 21:39

1 Answers1

1

Here's how to do it in mockk (I highly recommend switching away from Mockito, mockk is just so much easier):

import TestSingleton.staticMethod1
import io.mockk.every
import io.mockk.just
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.verify
import org.junit.jupiter.api.Test

internal object TestSingleton {
    @JvmStatic
    fun staticMethod1(str: String) {
        println("+++ === +++ TestSingleton.staticMethod(), $str")
        staticMethod2(str)
    }

    @JvmStatic
    fun staticMethod2(str: String) {
        println("+++ === +++ TestSingleton.staticMethod2(), $str")
    }
}

class StackSign {

    @Test
    fun test_staticMethod() {

        mockkStatic(TestSingleton::class)
        every { TestSingleton.staticMethod2("test") } just runs
        staticMethod1("test")
        verify(exactly = 1) { TestSingleton.staticMethod2("test") }
    }
}

BTW, add this to your build.gradle.kts

testImplementation("io.mockk:mockk:1.12.3")
PaulNUK
  • 4,774
  • 2
  • 30
  • 58