我怎样才能打开 class 来测试 class?
How can I open class only to test class?
我主要是一名 Java 开发人员,在用 kotlin 编写单元测试时想知道结构,
假设 kotlin
中没有
private
to restrict visibility to the file
internal
to restrict visibility to the module
如何打开 class 来测试 class?
我必须在 kotlin 中编写测试 class 还是对所有模块(内部)开放 class?
仅用于单元测试的 kotlin 打开方法的方法是什么?
编辑
在@bentolor 的 kotlin discuss 中找到类似的 question/request:
How am I supposed to do unit / whitebox testing properly? I want to write test code which tests class-internal functionality which I do not want to expose to other classes except my test class at all.
The package protected visibility is an excellent way to achieve this. Whereas Kotlin now requires me to make these methods effectively public and litter the visible API of my component all-over the project be able to test them.
In my view internal is more or less public as it has a much larger scope. Most projects have sth. around 1 - 5 “modules” in the Kotlin sense.
Really strongly asking/advocating for package-local visibility here.
正式地,不可能在 JVM 上诚实地执行此操作,因为 class 无法对可能的中间人子集开放。
但是可以通过以下技巧部分完成:
open class SomeClass internal constructor(val configurableParameter: Int) {
companion object {
private const val defaultInput = 123
fun create() = SomeClass(defaultInput)
}
}
此 class 的构造函数只能从同一模块(或测试)中调用。而 class 是 public,所以任何人都可以使用它。但是,对于外部模块,您只有两种 class 构造方式:伴随对象或反射。
最后,您无法在任何其他模块中继承此 class,因为构造函数是内部的。
仅供 Android 开发人员使用,有 AndroidX VisibleForTesting annotation
Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable
我主要是一名 Java 开发人员,在用 kotlin 编写单元测试时想知道结构,
假设 kotlin
中没有
private
to restrict visibility to the file
internal
to restrict visibility to the module
如何打开 class 来测试 class?
我必须在 kotlin 中编写测试 class 还是对所有模块(内部)开放 class?
仅用于单元测试的 kotlin 打开方法的方法是什么?
编辑
在@bentolor 的 kotlin discuss 中找到类似的 question/request:
How am I supposed to do unit / whitebox testing properly? I want to write test code which tests class-internal functionality which I do not want to expose to other classes except my test class at all.
The package protected visibility is an excellent way to achieve this. Whereas Kotlin now requires me to make these methods effectively public and litter the visible API of my component all-over the project be able to test them.
In my view internal is more or less public as it has a much larger scope. Most projects have sth. around 1 - 5 “modules” in the Kotlin sense.
Really strongly asking/advocating for package-local visibility here.
正式地,不可能在 JVM 上诚实地执行此操作,因为 class 无法对可能的中间人子集开放。
但是可以通过以下技巧部分完成:
open class SomeClass internal constructor(val configurableParameter: Int) {
companion object {
private const val defaultInput = 123
fun create() = SomeClass(defaultInput)
}
}
此 class 的构造函数只能从同一模块(或测试)中调用。而 class 是 public,所以任何人都可以使用它。但是,对于外部模块,您只有两种 class 构造方式:伴随对象或反射。
最后,您无法在任何其他模块中继承此 class,因为构造函数是内部的。
仅供 Android 开发人员使用,有 AndroidX VisibleForTesting annotation
Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable