How should I test Kotlin extension functions?

人走茶凉 提交于 2019-12-01 15:22:13

Well, to test a method, whether static or not, you call it as real code would do, and you check that it does the right thing.

Assuming this extension method, for example, is defined in the file com/foo/Bar.kt:

fun String.lengthPlus1(): Int {
    return this.length + 1
}

If you write your test in Kotlin (which you would typically do to test Kotlin code), you would write

assertThat("foo".lengthPlus1()).isEqualTo(4);

If you write it in Java (but why would you do that?)

assertThat(BarKt.lengthPlus1("foo")).isEqualTo(4);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!