Mockito Kotlin lateinit property X has not been initialized

Let’s look at this code:

class DemoTest {

	private lateinit var myClass: MyClass

	fun init() {

	fun test1() {
		LogManager.getLogger().info("1: $myClass")

	companion object {
		private val myClass = MyClass(1, 2)

		fun test2() {
			LogManager.getLogger().info("2: $myClass")

You see, that we have mock of myClass, that prepared by Mockito. In the same time we have object of MyClass inside companion object, that we prepared ourselves.

If you run this test with Kotlin 1.6.0 (and around) for instance, you’ll get this error:

lateinit property myClass has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property myClass has not been initialized

But, with Kotlin 1.4.32 and around, this stuff works fine. We see smth like this as output:

1: myClass$1
2: MyClass(a=1, b=2)

So the solution just is to change link name in companion object to avoid name overlapping:

companion object {
    private val myClassCustom = MyClass(1, 2)

That’s all.

Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at

If I saved your day, you can support me 🤝

Leave a Reply

Your email address will not be published. Required fields are marked *