在 Kotlin 中打开 getter 以将其与 Mockito 一起使用 - MissingMethodInvocationException

Open getter in Kotlin to use it with Mockito - MissingMethodInvocationException

Kotlin 可以自动为主要构造函数参数创建 getter(这很棒),并且默认情况下所有这些 getter 都是最终的(未打开)。 我有一个 class(在 Kotlin 中):

open class SongCategory(val id: Long,
                        val type: SongCategoryType,
                        val name: String? = null,
                        var displayName: String? = null,
                        var songs: List<Song>? = null) {
}

我想在一些 Mockito 测试中使用它(在 Java 中):

SongCategory songCategory = mock(SongCategory.class);
// the line below produces MissingMethodInvocationException
when(songCategory.getDisplayName()).thenReturn("Dupa");

这会产生 MissingMethodInvocationException 因为 Mockito 需要打开模拟的 class (不是最终的)并且模拟的方法 getDisplayName() 只需要打开但它不是。

我无法打开此 getter 或创建另一个覆盖 getter,因为它与为构造函数自动创建的最终 getter 冲突。

我可以将这些所有参数移动到辅助构造函数并分别创建所有属性和 getter。但是,如果我必须编写与 Java 中相同的样板代码,那么使用 Kotlin 有什么意义呢?

有什么方法可以将 Mockito 与 Kotlin 编译的 getter 一起使用吗?

不要 open 你的 class 只是为了测试。相反,请尝试使用编译器插件为您执行此操作。您可以在这里阅读:https://kotlinlang.org/docs/reference/compiler-plugins.html#all-open-compiler-plugin

完成后,您将能够像使用其他任何东西一样使用 Mockito Java class。

你可以尝试用PowerMock处理final方法

或者 Javasist 及其 javassist.Modifier

其实我发现打开getter的语法很简单(虽然官方文档里没有):

open class SongCategory(...
                        open var displayName: String? = null,
                        ...) {
}

这会为 属性 打开 getter 和 setter。

我同意@Unknown 的观点,有些地方可以使用 kotlin-allopen 插件。但在这种情况下,由于您要做的只是模拟 Kotlin class(未打开),您只需添加 mockito-inline 插件即可。

将以下内容添加到您的build.gradle

testImplementation 'org.mockito:mockito-core:2.13.0' // use the latest version
testImplementation 'org.mockito:mockito-inline:2.13.0'