2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Mockkでio.mockk.MockKException: no answer foundのときスタティックメソッドかをみる

Posted at

はじめに

mockkの使い方で困ったことがあり、頻繁に遭遇しそうなのでまとめます

問題

gateway_test.kt
        @Test
        @DisplayName("サンプルテスト")
        fun testSample() {

            val targetIndex: IndexName = mockk()
            val companies: CompanyDocument = mockk()
            val jsonCompanies: List<JSONCompany> = mockk()
            val documents: Documents = mockk()

            every { companies.toJSON() } returns jsonCompanies
            every { jsonCompanies.toDocuments(targetIndex) } returns documents

            target.indexing(targetIndex, companies)
            
            verify { companyEs.index(documents) }
        }
Gateway.kt

    override fun indexing(targetIndex: IndexName, companyDocument: CompanyDocuments) {
        companyDocument.toJSON().toDocuments(targetIndex).let(companyEs::index)
    }
Extension.kt
fun CompanyDocument.toJSON(): JSONCompany = JSONCompany(
    name: name.name
)

fun List<JSONCompany>.toDocuments(indexName: IndexName): Documents {
    return Documents(
        indexName.value,
        this
    )
}

このようなテストを実装して実行したところ以下のエラーが出ました

22:45:45.389 [main] WARN io.mockk.impl.recording.states.StubbingAwaitingAnswerState -- Failed to set backing field (skipping)
java.lang.IllegalArgumentException: Can not set final java.lang.String field jp.sample.IndexName.value to jp.sample.entity.Documents
	at java.base/jdk.internal.reflect.FieldAccessorImpl.throwSetIllegalArgumentException(FieldAccessorImpl.java:228)
	at java.base/jdk.internal.reflect.FieldAccessorImpl.throwSetIllegalArgumentException(FieldAccessorImpl.java:232)
	at java.base/jdk.internal.reflect.MethodHandleObjectFieldAccessorImpl.set(MethodHandleObjectFieldAccessorImpl.java:115)

 

解決方法

インスタンスに対してのモックであればこれでよいのですが、今回はクラスのスタティックメソッドに対してのモックなので以下を追加する必要がありました

gateway_test.kt
       @Test
       @DisplayName("サンプルテスト")
       fun testSample() {

           mockkStatic("jp.sample.extension.ExtensionsKt")
           val targetIndex: IndexName = mockk()
           val companies: CompanyDocument = mockk()
           val jsonCompanies: List<JSONCompany> = mockk()
           val documents: Documents = mockk()

           every { companies.toJSON() } returns jsonCompanies
           every { jsonCompanies.toDocuments(targetIndex) } returns documents

           target.indexing(targetIndex, companies)
           
           verify { companyEs.index(documents) }
       }

おわりに

スタティックのときは注意ということだけ覚えておきたいです

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?