Test in Kotlin cannot access protected method

protected in Java is not the same as in Kotlin.

In Java, everything in the same package can access a protected method. See In Java, difference between default, public, protected, and private

In Kotlin, protected means that you can only access it in the same class or any subclass of it. See Visibility Modifiers - Kotlin

The only possible way is to use the internal modifier and make the method visible to your tests in the same module.


Since Kotlin reduce visibility on protected (in compare to Java) by not allowing package access, the best option I could find is to workaround with reflection (since this is for testing I see no reason why not)

private fun invokeHiddenMethod(name: String) {
    val method = sut.javaClass.getDeclaredMethod(name)
    method.isAccessible = true
    method.invoke(testSubject)
}