くうと徒然なるままに

モバイルアプリを作りながらバックエンドも作っています。

Kotlin, JUnit で パラメタライズドテストを書く

こちらで書いたコードに対してパラメタライズドテストを書いていきます。

kuxumarin.hatenablog.com

環境

testImplementation 'junit:junit:4.12'

コード

Java で書くときは、Static な感じで書いてたのは companion object にしました。

@RunWith(Parameterized::class)
internal class HtmlUtilSpec(private val HtmlText: String, private val planeText: String) {

    @Test
    fun ConvertHtmlToPlaneText() {
        Assert.assertEquals(HtmlText.removehtmlTag(), planeText)
    }

    companion object {
        @Parameterized.Parameters
        @JvmStatic
        fun data() = listOf(
                arrayOf("hello neet", "hello neet"),
                arrayOf("<h1>hello</h1>", "hello"),
                arrayOf("<br>hello\n", "hello"),
                arrayOf("<h1>hello<h1>", "hello")
        )
    }
}

参考元

Parameterized JUnit4 test example in Kotlin · GitHub