# 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:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [September 1, 2022, 1:27pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/22 "2022-09-01T13:27:08Z")

</div>

Yeah, looks inconvenient. Is the method how to properly test protected methods in final classes already found ?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [September 3, 2022, 12:06am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/23 "2022-09-03T00:06:45Z")

</div>

You could use [opt-in annotations](https://kotlinlang.org/docs/opt-in-requirements.html) and simply do a module-wide opt-in for the test module only.  
E.g.:

```kotlin
@RequiresOptIn("This method is `internal` only for unit tests")
annotation class VisibleForUnitTests

class Foo {
  @VisibleForUnitTests
  internal fun bar() = TODO()
}

// Add compiler option "-opt-in=org.mylibrary.VisibleForUnitTests" in gradle config for the test module
// Then in tests
class FooUnitTests {
  @Test fun barTest() {
    Foo().bar() // Doesn't even need a `@OptIn(VisibleForUnitTests::class)` because of the compiler argument
  }
}

```

---

<div class="post-metadata">

**Author:** ![kimnicole.dev](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kimnicole.dev](https://discuss.kotlinlang.org/u/kimnicole.dev)\
**Post date:** [November 5, 2022, 12:55am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/24 "2022-11-05T00:55:08Z")

</div>

@franco why do you make yourself so complicated? one way to do is you can create a function that will return your protected abstract function then you can set that to `@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE | NONE)`

---

<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:** [November 9, 2022, 8:29am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/25 "2022-11-09T08:29:37Z")

</div>

@kyay10 so you are actually creating your own `@VisibleForTesting` (which is only usable for Android if I get it right?) - nice idea!  
But that wouldn’t work for protected functions, only for private ones, unless I am missing something?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [November 10, 2022, 7:37pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/26 "2022-11-10T19:37:44Z")

</div>

Well the point is that you change your protected and private functions to be `internal` and annotated with `VisibleForTesting`, which forces you everywhere else to not use those functions because you’ll read the opt-in message and realise that you shouldn’t opt-in to using those methods. Then in your gradle config you make the test sourceset automatically opt in to `VisibleForTesting` through the compiler option I mentioned, and hence your test code will automatically be able to access all `VisibleForTesting` methods without opting in manually.

---

<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:** [November 11, 2022, 8:14am UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/27 "2022-11-11T08:14:13Z")

</div>

@kyay10 \> you change your protected and private functions to be `internal`

Well, as I said before, this of course works for private methods but NOT for protected.  
But when you change `protected` to `internal`, you actually lose visibility which is not an option. `protected` method are meant to be used from outside, even outside your module.

So… still no solution ☹

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [November 11, 2022, 8:40pm UTC](https://discuss.kotlinlang.org/t/how-to-test-protected-function/6133/28 "2022-11-11T20:40:00Z")

</div>

I haven’t done it myself but I assume [https://mockk.io/](https://mockk.io/) would work. You’d make a spy on your class, possibly abstract or an interface, do some verify declarations and It would magically (\*bytecode manipulation) work like all things in Mockk–it’s quite a library.

Mockk supports mocks and spies with private methods as well.

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