Webエンジニアのメモ帳

技術的な話を中心に書いています。

Kotlinのテストで「lateinit property 〇〇 has not been initialized」のエラーが出た場合の対処法

Kotlinのテストで「lateinit property 〇〇 has not been initialized」のエラーが出た。〇〇は「@MockK」アノテーションによりモック化しているクラス。

この場合には、以下のメソッドを追加することでエラーが解消される。

@Before
fun setUp() {
    MockKAnnotations.init(this)
}

ひとつ注意点として、@BeforeEachではなく@Beforeをつけないとエラーは消えない。

@Beforeは以下のimport文の記述により使えるようになる。

import org.junit.Before