# How to test protected function

**URL:** <https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133>\
**Category:** Support\
**Created:** [January 7, 2018, 4:58pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133 "2018-01-07T16:58:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![knolle](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@knolle](https://discuss.kotlinlang.org/u/knolle)\
**Post date:** [January 7, 2018, 4:58pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/1 "2018-01-07T16:58:10Z")

</div>

In Java `protected ` members are also visible in the whole package and thus could be tested right away.  
In Kotlin however, protected members are only visible in subclasses.

How are they supposed to be tested in unit tests?  
From my point of view, `protected` members are part of the API and really should be tested.

---

<div class="post-metadata">

**Author:** ![ilogico](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilogico/32/3725_2.png) [@ilogico](https://discuss.kotlinlang.org/u/ilogico)\
**Post date:** [January 7, 2018, 5:20pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/2 "2018-01-07T17:20:41Z")

</div>

Subclass your class and call super.

---

<div class="post-metadata">

**Author:** ![knolle](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@knolle](https://discuss.kotlinlang.org/u/knolle)\
**Post date:** [January 7, 2018, 7:07pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/3 "2018-01-07T19:07:21Z")

</div>

That works, of course.

But that means your test class needs to extend the class under test which is quite unusual in my opinion.

---

<div class="post-metadata">

**Author:** ![ilogico](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilogico/32/3725_2.png) [@ilogico](https://discuss.kotlinlang.org/u/ilogico)\
**Post date:** [January 7, 2018, 8:05pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/4 "2018-01-07T20:05:21Z")

</div>

As you said, protected functions are part of the API, so it makes sense to test them.  
IMO, it makes sense to test them the way they are going to be used.

---

<div class="post-metadata">

**Author:** ![knolle](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@knolle](https://discuss.kotlinlang.org/u/knolle)\
**Post date:** [January 7, 2018, 10:59pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/5 "2018-01-07T22:59:59Z")

</div>

It would look like this:

```
open class A {
    protected fun f() = 3
}

class ATest : A() {
    @Test fun testF() {
        val testee = ATest()
        assertEquals(3,testee.f())
    }
}

```

Maybe its just me, but I find it strange that the “testee” is the test class itself…

---

<div class="post-metadata">

**Author:** ![forinil2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/forinil2/32/2465_2.png) [@forinil2](https://discuss.kotlinlang.org/u/forinil2)\
**Post date:** [January 8, 2018, 7:08am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/6 "2018-01-08T07:08:06Z")

</div>

As far as I know nothing is stopping you from doing something like this:

```kotlin
open class A {
    protected fun f() = 3
}

class B: A() {
    public fun fPublic() = f()
}

class ATest {
    @Test fun testF() {
        val testee = B()
        assertEquals(3,testee.fPublic())
    }
}

```

which gives you sepration of tested code form code of the test.

---

<div class="post-metadata">

**Author:** ![knolle](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@knolle](https://discuss.kotlinlang.org/u/knolle)\
**Post date:** [January 8, 2018, 8:29am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/7 "2018-01-08T08:29:14Z")

</div>

Well, to be honest, thats exactly how I did it.  
But I didn’t like it, creating a wrapper class and a wrapper function.  
So I asked myself, if I was missing something…

Thanks anyway for all your comments!

---

<div class="post-metadata">

**Author:** ![franco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/franco/32/7390_2.png) [@franco](https://discuss.kotlinlang.org/u/franco)\
**Post date:** [March 14, 2018, 12:36am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/8 "2018-03-14T00:36:02Z")

</div>

I’m having this same issue. I understand that you can extend the class when it’s `open` or `abstract` (as shown in comments above), but what about when it’s final?

Sure, you can change your class to be `open` because you want to test the `protected` methods inside it, but then your code architecture is influenced by the tests, which I don’t like. The whole reason why Kotlin classes are not open by default is so that we can make them open only when it makes sense, and for me, it doesn’t make sense to make it open just to test it.

Is there any other option?

---

<div class="post-metadata">

**Author:** ![HughG](https://avatars.discourse-cdn.com/v4/letter/h/35a633/32.png) [@HughG](https://discuss.kotlinlang.org/u/HughG)\
**Post date:** [March 16, 2018, 4:53pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/9 "2018-03-16T16:53:02Z")

</div>

If you have a final class, protected members don’t make any sense (and I hope are rejected by the compiler). If you want to test internal methods, that’s another matter.

---

<div class="post-metadata">

**Author:** ![franco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/franco/32/7390_2.png) [@franco](https://discuss.kotlinlang.org/u/franco)\
**Post date:** [March 16, 2018, 11:34pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/10 "2018-03-16T23:34:12Z")

</div>

Hi Hugh,

I agree with that in most cases, but I have a scenario where this actually makes sense, e.g.:

```
abstract class A {
    public fun doStuff() {
        doSomething()
    }

    protected abstract fun doSomething()
}

class B : A() {
    override fun doSomething() {
        ...
    }
}

```

and what I want is to create a unit test for `B.doSomething()`

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 17, 2018, 12:10pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/11 "2018-03-17T12:10:19Z")

</div>

I guess one thing you could do is use the [all open](https://kotlinlang.org/docs/reference/compiler-plugins.html#all-open-compiler-plugin) compiler plugin while testing and disabling it when deploying. It should be possible to set something like that up using gradle/maven. The only problem is that you no longer get any errors when inheriting from a sealed class during development and testing. Not sure if this is really the best thing to do, but if you want to check internal methods thats the only idea I have.

---

<div class="post-metadata">

**Author:** ![franco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/franco/32/7390_2.png) [@franco](https://discuss.kotlinlang.org/u/franco)\
**Post date:** [March 17, 2018, 9:35pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/12 "2018-03-17T21:35:14Z")

</div>

Thanks for the recommendation Wasabi375! I was not aware of that plugin. I will look into it in more detail when I have time, but if it’s too much work to set up only for testing (without affecting the development compilation) then I probably won’t use it.

For the moment what I did is set the `protected` methods I need to unit test as `internal` and added a `TODO` so I can make them protected once I find a better solution. It’d be great to get some official support for this, maybe a plugin like the one you mentioned but that changes the visibility of methods or classes only for testing.

---

<div class="post-metadata">

**Author:** ![HughG](https://avatars.discourse-cdn.com/v4/letter/h/35a633/32.png) [@HughG](https://discuss.kotlinlang.org/u/HughG)\
**Post date:** [March 22, 2018, 5:58pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/13 "2018-03-22T17:58:10Z")

</div>

Ah, I see: you want to test final overrides of a protected method. In that sort of case, I wouldn’t try to test the protected method directly. Instead, I’d do two things.

1. Write a test which uses a spy subclass to check that the base class is calling the protected method as it should. (For example, if the base class should cache the result, then base method calls after the first shouldn’t reach the subclass.)
2. Write an abstract test for the base class and override it for each subclass which needs to be tested. The test subclasses would change the setup and/or add more tests to cover the effects of the method overrides.

Alternatively, change from inheritance to composition - pull out the overridable parts into a Strategy object, and test those Strategies as separate classes.

---

<div class="post-metadata">

**Author:** ![franco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/franco/32/7390_2.png) [@franco](https://discuss.kotlinlang.org/u/franco)\
**Post date:** [March 22, 2018, 9:04pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/14 "2018-03-22T21:04:31Z")

</div>

Thanks for the help @HughG 🙂

Would you mind giving a short example of point number 2? I’m not sure I understand what you meant exactly and a short code example would be greatly appreciated.

Changing from inheritance to composition would certainly make testing easier. Unfortunately, given the nature of the relationship between the classes, using composition would make the code more complicated and it’d make less logical sense.

---

<div class="post-metadata">

**Author:** ![theartofme](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@theartofme](https://discuss.kotlinlang.org/u/theartofme)\
**Post date:** [August 13, 2018, 12:15am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/15 "2018-08-13T00:15:01Z")

</div>

You can change the visibility when overriding - you can’t change it to `internal` (apparently you can’t make visibility more restricted), but you can change it to `public`, so this would work:

```auto
class B : A() {
    public override fun doSomething() {
        ...
    }
}

```

This will be fine in terms of encapsulation if `class B` already has restricted visibility so can’t be accessed from outside your module.

---

<div class="post-metadata">

**Author:** ![franco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/franco/32/7390_2.png) [@franco](https://discuss.kotlinlang.org/u/franco)\
**Post date:** [August 13, 2018, 10:42pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/16 "2018-08-13T22:42:43Z")

</div>

@theartofme that’s actually not good from my point of view, as I’d still be exposing this method to the rest of my module, which is not something I want to do (if that was the case I’d just make the original method internal). I want the protected method to stay with the same visibility and not be accessible from outside the base or child classes, except for testing.

---

<div class="post-metadata">

**Author:** ![marco.s](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/marco.s/32/6781_2.png) [@marco.s](https://discuss.kotlinlang.org/u/marco.s)\
**Post date:** [September 23, 2019, 11:02am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/17 "2019-09-23T11:02:52Z")

</div>

Any official updates on this? I feel like we’re not dealing with the elephant in the room here.  
All of the proposed solutions imply modifying production code for testing, or resorting to external solutions (`AllOpen`) which IMHO are pretty dangerous to use on big projects with several developers working on the same codebase.

Right now, this limitation (or change of spec, as it might be more correctly defined) is getting in the way of writing unit tests quickly and effectively, especially in the case of final classes with protected functions (which is perfectly legal and reasonable when they override `protected` functions from their `open` or `abstract` superclasses). On Android, one could use `@VisibleForTesting` to get static analysis to help with not calling previously-protected functions from production code; that’s still not great with larger classes, though.  
I can easily see devs giving up on using proper logic encapsulation for the sake of being able to write tests more quickly. This can’t lead to anything good 🙂

---

<div class="post-metadata">

**Author:** ![StanislavKozlovski](https://avatars.discourse-cdn.com/v4/letter/s/dbc845/32.png) [@StanislavKozlovski](https://discuss.kotlinlang.org/u/StanislavKozlovski)\
**Post date:** [May 10, 2020, 7:20pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/19 "2020-05-10T19:20:45Z")

</div>

I have to say I’m also disappointed that this doesn’t exist yet. Kotlin is in need of a package-private modifier.

Numerous articles have been written on the matter.

---

<div class="post-metadata">

**Author:** ![luhtonen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luhtonen/32/6825_2.png) [@luhtonen](https://discuss.kotlinlang.org/u/luhtonen)\
**Post date:** [May 28, 2020, 8:49am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/20 "2020-05-28T08:49:36Z")

</div>

package-private modifier is just a fake. Nothing stops developer from creating same package structure in his project and to get full access to all package protected members.

Kotlin have _internal_ modifier, which works fine within Kotlin code, but I have problem to access those functions from Java unit tests.

---

<div class="post-metadata">

**Author:** ![Loke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Loke](https://discuss.kotlinlang.org/u/Loke)\
**Post date:** [May 28, 2020, 9:13am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/21 "2020-05-28T09:13:34Z")

</div>

“Fake” or not, it’s very useful. How many people actually suffered from the supposed “fakeness” of package visibility in Java? I’d dare to guess that that number is very small.

Just because the feature doesn’t actively enforce some kind of code security doesn’t mean it isn’t really useful.

As for myself, my Kotlin code mostly uses public visibility anyway, since I’m fed up with having to deal with the limitations of the existing mechanisms.

[Next page](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133.md?page=2)
