fbpx

How to mock final classes in Kotlin with Mockito?

Let’s consider the following system under test:

class Repository {
  fun getAll(): List<String> {
    // retrieve or create list of strings somehow
  }
}

// return only entries containing letter "g"
class UseCase(private val repository: Repository) {
  fun execute(): List<String> {
    return repository.getAll().filter { it.contains("g") }
  }
}

And following test method:

class UseCaseTest {
  @Test
  fun `it should return elements containing letter g`() {
    val mockRepository: Repository = mock {
      on { getAll() } doReturn listOf("kotlin", "testing", "blog")
    }

    val useCase = UseCase(
      repository = mockRepository
    )

    val result = useCase.execute()

    result.size shouldBe 2
  }
}

if you are unsure how to use Mockito with Kotlin, check https://androidpro.io/using-mockito-in-kotlin-projects/

After running this test it is likely that you will encounter the following error:

Cannot mock/spy class com.kotlintesting.edu.Repository
Mockito cannot mock/spy because :
 - final class
  1. Classes marked as “final” should not be extended
  2. Kotlin default modifier translates to “final” in Java
  3. Classes in Kotlin are “final” unless specified otherwise (open class, abstract class)
  4. Mocking frameworks usually extend your class to do something with implementation details.

While the preferred solution is to use an interface for dependency, there is solution that will allow us to mock final classes with Mockito. To enable mocking final classes, configure MockMaker:

Configure MockMaker

In app/src/test/resources/mockito-extensions add file:

org.mockito.plugins.MockMaker

With contents:

mock-maker-inline

org.mockito.plugins.MockMaker content

MockMaker location in project files

If you add this file in proper place and run tests again, class Repository will be properly mocked and error Mockito cannot mock/spy because your class is final class will be there no more.


more insights

Uncategorized
Jarosław Michalik

#kotlinDevChallenge 2023 summary

The KotlinDevChallenge was not just a competition, but a journey of learning, growth, and community engagement. Every participant in this challenge is a winner in

Read More »

AndroidPro newsletter 🚀

join 3057 developers reading AndroidPro newsletter every week

👉 tips & tricks, articles, videos

👉 every Thursday in your inbox

🎁 bonus on signup – Clean Architecture blueprint

brought to you by Jarek Michalik, Kotlin GDE

You can unsubscribe any time. For details visit our privacy policy.

android pro newsletter tips and tricks how to learn android