I am trying to mock a private function call in kotlin with mockk. Running into the following error:
Class is final. Put @MockKJUnit4Runner on your test or add MockK Java Agent instrumentation to make all classes 'open'
I fully understand the implications of trying to test a private method, however at this point its what I need to do.
Code:
class ClassToTest {
private fun privateMethod(text: String): String {
return "Hello $text"
}
}
class TestClass {
@Before
fun setup() {
MockitoAnnotations.openMocks(this)
}
@Test
fun testSplitNumbers() {
val clazz = spyk<ClassToTest>()
val method = clazz.javaClass.getDeclaredMethod("privateMethod", String::class.java)
method.isAccessible = true
val result = method.invoke(clazz, "Test") as? String
Assert.assertEquals("Hello Test", result)
}
}