测试替身的 Kotlin 哲学是什么?

What is the Kotlin philosophy of test doubles?

test doubles 的 Kotlin 哲学是什么?默认情况下,所有 classes 和函数都是密封的(不是 open),像 Mockito 这样的模拟框架显然不是第一个 class 公民。

关于 problem, and also about possible solutions 已经写了很多,但是 Kotlin 的设计者对于双精度测试有什么想法?是假货和存根,还是您应该扮演自己的间谍和模拟角色?

Kotlin 设计的很大一部分是考虑到 Joshua Bloch 的 Effective Java,意思是默认关闭、不变性等。

因此,Java也存在这些问题。这些问题的解决方案过去和现在都尽可能使用接口,这样您就可以为您的生产代码提供测试替身或任何其他实现。

对于测试,这些可能是模拟、伪造、存根或您拥有的任何东西。请记住,例如,编写良好的内存存储库在测试中比必须为此设置模拟更容易处理 class。

像 Mockito 这样的框架是第一个 class 公民,包括直接提到它们作为 Kotlin 1.0.6 中发布的 all-open 编译器插件的原因之一。您对 problems/solutions 的引用已过时,如果这些作者更新了他们的帖子就好了

从那个release announcement你会看到 Mockito 被特别调用:

The all-open compiler plugin makes classes annotated with a specific annotation and their members open without the explicit open keyword, so it becomes much easier to use frameworks/libraries such as Spring AOP or Mockito. You can read the detailed information about all-open in the corresponding KEEP.

We provide all-open plugin support both for Gradle and Maven, as well as the IDE integration.

该插件可用于使用命令行编译器、IDE 编译器以及来自 Gradle 和 Maven。

其他参考资料:

除了@nhaarman 的评论,另一个选项是Mockito-Kotlin。我喜欢 Kotlin 的 类 默认是最终的,所以 all-open 编译器插件对我来说不是一个好的选择。