2

I'm kind of new with mockito so I don't know if this behavior is normal or not.

This is a simple example class:

open class Example( val example2: Example2) {

    fun getStuff(fileName: String) : String {
           return example2.getFileExtension(fileName)
    }
}

open class Example2(val fileUtils: FileUtils) {

    fun getFileExtension(fileName: String): String {
        return fileUtils.getExtension(fileName)
    }
}

So when I tried to test it with this code:

class ExampleTest {
    lateinit var example: Example
    val example2 = mock(Example2::class.java)

    @Before
    fun init() {
        example = Example(example2)
    }

    @Test
    fun getFileExtensionTest() {
        val resultExpected = "jpg"

        `when`(example2.getFileExtension(ArgumentMatchers.anyString())).thenReturn("jpg")

        assertThat(example.getStuff("hello.jpg"), `is`(resultExpected))
    }
}

I'm getting a NullPointerException in return fileUtils.getExtension(fileName) when 'when' is executed

So 'when' is executing the function that suppose to be mocked.

Buntupana
  • 984
  • 1
  • 15
  • 33

1 Answers1

0

I got it!!

Kotlin makes a function final by default and mockito can't mock final functions/classes. So there are two solutions here:

Buntupana
  • 984
  • 1
  • 15
  • 33