正如我们可能知道的那样,默认情况下,一旦定义了Kotlin类,它就是最终类,除非它被显式声明为open.

This would post a challenge when we want to Mock it using Mockito. We need to explicitly declare it as open. Is there a way we could avoid declaring it as open while able to Mock it for our testing?

推荐答案

Mockito2现在也可以模拟期末课程.

However, this feature is opt-in, so you need to enable it manually.
To do so, you need to define a file /mockito-extensions/org.mockito.plugins.MockMaker containing the line mock-maker-inline

See e.g.
http://hadihariri.com/2016/10/04/Mocking-Kotlin-With-Mockito/ or https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#unmockable
for a quick introduction

顺便说一句,目前这doesn't work for android

Kotlin相关问答推荐

为什么不';Kotlin是否在数据类构造函数中隐式分配null值可为null的字段?

Kotlin 说不需要强制转换,但删除后会出现新警告

为什么使用 return instance ?: synchronized(this) { instance ?: PreferenceParameterState(context) } 时无法获得单例?

在协程上下文中重新抛出异常

从 HashMap 检索时的 NPE,即使 containsKey() 在多线程环境中返回 true

kotest 更改环境变量

Android 导航组件 - 向上导航打开相同的片段

SpringBoot 2.5.0 对于 Jackson Kotlin 类的支持,请在类路径中添加com.fasterxml.jackson.module: jackson-module-kotlin

Kotlin 类的调用方法

如何在 kotlin 中生成 json 对象?

Kotlin 创建snackbar

Kotlin DataBinding 将静态函数传递到布局 xml

如何使用kotlin中的反射查找包中的所有类

在调用Kotlin数据类中的超类构造函数之前访问函数

如何在Kotlin中使用ViewModelProviders

类型不匹配:推断类型为 LoginActivity 但应为 LifecycleOwner

如何让数据类在Kotlin中实现接口/扩展超类属性?

接口中的属性不能有支持字段

如何在Kotlin中使方法param可变?

Kotlin中对象和数据类的区别是什么?