Test in Kotlin cannot access protected method
Asked Answered
C

3

14

I want to test class B:

class B : A {
    override fun init() {
        // do work here
    }
}

class A {
    protected fun init() { } // will be called by internal logic
}

and in Java there is no problem to call: b.init() within test method (test class is in the same package as test subject), but in Kotlin compiler complains:

Cannot access 'init': it is protected in 'B'

@Test
fun `checks init`() {
    val b = B()
    b.init()
    // assert work done
}

Why isn't it working? How can this be workaround (I want to avoid making method public)?

Conservancy answered 15/1, 2017 at 16:4 Comment(2)
Possible duplicate of What access modifier for testable helper methods?Faradmeter
@JBNizet changing visibility on overridden method is with internal not possible and with public not really acceptable - especially since I can do it in JavaConservancy
C
10

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)
}
Conservancy answered 17/1, 2017 at 11:1 Comment(0)
O
13

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.

Omsk answered 15/1, 2017 at 17:23 Comment(2)
Class A is actually part of library, so cannot be changed and when I tried to put internal on overridden method it's not allowedConservancy
@KamilSeweryn if you think this info is relevant to your question (arguably, it is, since the context you provide basically invalidates this answer), you should probably add it to the question instead; comments are transient by definition on SO (yeah, I know I'm 6 years late to the party here :D)Weaponeer
C
10

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)
}
Conservancy answered 17/1, 2017 at 11:1 Comment(0)
L
1

There is one more option - create wrapper subclass and override the test target method with public visibility:

class FooTest {
    
    @Test
    fun testFoo() {
        val foo = FooWrapper() // Instead of Foo
        foo.protectedMethod()
    }

    private class FooWrapper : Foo() {
        override public fun protectedMethod() {
            super.protectedMethod()
        }
    }
}

This way, you wont need to modify the visibility of the original method, and will not use reflection. Also, the auxiliary wrapper class will remain invisible outside the test class.

Levirate answered 12/1 at 16:11 Comment(1)
Nice idea but wouldn't work if Foo is final.Invincible

© 2022 - 2024 — McMap. All rights reserved.