25

How to test private methods in Kotlin? I tried to add @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) from androidx.annotation.VisibleForTesting but it doesn’t make my function private

This is how I’m using it

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun doSomething() {}

[EDIT]

I understand that I shouldn't test private methods, but it's now always trivial. What about below case.

I have a CsvReader class

class CsvReader(private val inputStream: InputStream, private val separator: String = "\t") {
    fun read(): List<String> {
        return read(inputStream.bufferedReader())
    }
    private fun read(bufferedReader: BufferedReader): List<String> {
        val line = bufferedReader.use { it.readLine() } // `use` is like try-with-resources in Java
        return parse(line)
    }
    private fun parse(line: String): List<String> {
        return line.split(separator)
    }
}

And I wrote tests for it

class CsvReaderTest {
    private val stream = mock<InputStream>()
    private val reader = CsvReader(stream)
    private val bufferedReader = mock<BufferedReader>()
    @Test
    fun read() {
        whenever(bufferedReader.readLine()).thenReturn("Jakub\tSzwiec")
        reader.read(bufferedReader) shouldEqual listOf("Jakub", "Szwiec")
    }
    @Test
    fun readWhenEmpty() {
        whenever(bufferedReader.readLine()).thenReturn("")
        reader.read(bufferedReader) shouldEqual listOf("")
    }
    @Test
    fun throwIOExceptionWhenReadingProblems() {
        whenever(bufferedReader.readLine()).thenThrow(IOException::class.java)
        val read = { reader.read(bufferedReader) }
        read shouldThrow IOException::class
    }
}

Unfortunately, for tests I need to call private function fun read(bufferedReader: BufferedReader): List<String> because when mocking File, file.bufferedReader gives NullPointerException Unable to mock BufferedWriter class in junit

7
  • 3
    An annotation cannot make a function be private. It can help Android Studio warn you about using that function from things other than test code, but that's about it. Aug 26, 2018 at 22:53
  • 5
    You can't directly test private methods, and you can't make a method private any other way than the keyword private. Either make them internal or only test public API. Aug 26, 2018 at 23:19
  • 2
    Testing a private function is a really bad practice. Always test the public api; don't tie your test to implementations details.
    – Mik378
    Aug 27, 2018 at 8:05
  • 1
    For the record: splitting by "," is like 5% of what a CSV parser does for you. Seriously: don't do such things. Use an existing robust CSV parser library. You invent the wheel, and you will repeat all the mistakes that people run into when doing that. Believe me, I have been there to. Parsing arbitrary CSV correctly is hard.
    – GhostCat
    Aug 28, 2018 at 9:20
  • 1
    I know @GhostCat. That's the interview assignment :)
    – jakub
    Aug 28, 2018 at 14:12

3 Answers 3

13

You can use java reflections:

To test the method:

class ClassUnderTest {
      private fun printGreetings(name: String): String {
        return "Hello, $name"
      }
}

That's enough:

  private val classUnderTest = spyk(ClassUnderTest()) 

  @Test
    fun `should return greetings`() {
      val method = classUnderTest.javaClass.getDeclaredMethod("printGreetings", String::class.java)
      method.isAccessible = true
      val parameters = arrayOfNulls<Any>(1)
      parameters[0] = "Piotr"

      assertEquals("Hello, Piotr", method.invoke(classUnderTest, *parameters) )
  }
5
  • 1
    What is spyk here? Oct 21, 2021 at 7:17
  • 2
    FYI: Works without spyk
    – mmccabe
    Jan 1, 2022 at 16:28
  • 1
    spyk is coming from MockK testing library , see this mockk.io/#spy Jan 22, 2022 at 17:44
  • @mmccabe, in my case it works for spyk. If I set classUnderTest = mockk<ClassUnderTest>(relaxed = true), then method.invoke returns "". If you want to call the method without returning a result, it would be enough.
    – CoolMind
    Mar 31, 2022 at 12:51
  • Doesn't work for me. "Failed to create proxy for com.test.ClassUnderTest. Class is final." May 11 at 14:13
8

like this:

fun callPrivate(objectInstance: Any, methodName: String, vararg args: Any?): Any? {
        val privateMethod: KFunction<*>? =
            objectInstance::class.functions.find { t -> return@find t.name == methodName }

        val argList = args.toMutableList()
        (argList as ArrayList).add(0, objectInstance)
        val argArr = argList.toArray()

        privateMethod?.apply {
            isAccessible = true
            return call(*argArr)
        }
            ?: throw NoSuchMethodException("Method $methodName does not exist in ${objectInstance::class.qualifiedName}")
        return null
    }

you need to pass instance of the object you want the method to be called on, method name and required arguments

1
  • This is actually very cool, if you really really really have to test private functions, or to learn more about Kotlin reflection. Generally, though, it's advised to use the adequate visibility modifier internal instead of private.
    – ikaerom
    Nov 23, 2022 at 18:48
2

There is only one answer to this: don't even try.

You write source code to communicate intent. If you make something private then that something is an internal implementation detail. It is subject to change the very next second.

10
  • 30
    what if you still want to test that this private function works correctly? Mar 8, 2020 at 21:27
  • 4
    Then you should test the usage paths that lead to that private detail.
    – GhostCat
    Mar 23, 2020 at 11:31
  • 16
    The does not answer the question, OP was not asking whether or not it is a good idea to test private methods, but rather how to do so.
    – Attila
    Aug 30, 2020 at 22:24
  • 5
    @Attila There are always other people that give the "technical" answer to such questions. But remember: content here is not only written for the OP asking the question, but for all future readers. And thing is: it is a bad idea to test private methods. But unfortunately: a lot of people do not understand that part. You can write your own answer, and explain to people how to correctly shot themselves into their feet. I prefer to tell them: avoid shooting yourself in the foot, because that is a bad thing to do. What is more helpful in the end, the readers can decide themselves.
    – GhostCat
    Aug 31, 2020 at 6:56
  • 2
    @GhostCat, I appreciate the rational behind your answer, it does make sense indeed and brings added value. However, shouldn't be your "non technical/direct" answer added as a comment only to the OP question rather than a proper answer to the question then?
    – Attila
    Aug 31, 2020 at 8:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.