概述
应用
@DataPoint
或
@DataPoints
到Kotlin类中的属性会导致“DataPoint字段samples must be public”错误。
说明
青少年使用理论
@数据点
和
@数据点
用于标记样本数据的注释,这些样本数据被收集并传递给可以将其作为参数的各个测试。当这些注释应用于Kotlin属性(这些属性也必须在@JvmStatic中注释并在伴随对象中声明)时,测试用例失败,并出现错误“DataPoint field samples must be public”。默认情况下,Kotlin属性应该是公共的。此外,显式地添加“public”修饰符没有影响。
示例代码
import org.junit.runner.RunWith
import org.junit.experimental.theories.Theories
import org.junit.experimental.theories.DataPoints
import org.junit.experimental.theories.Theory
import org.junit.Assume.*
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
@RunWith(Theories::class)
class SampleTheories {
companion object {
@JvmStatic
@DataPoints
public val samples = listOf(
-8, -1, 0, 1, 2, 4, 8
)
}
@Theory
fun triangleInequality(a:Int, b:Int) {
assumeThat(a, `is`(greaterThan(0)))
assumeThat(b, `is`(greaterThan(0)))
assertThat(a+b, `is`(greaterThan(a)))
assertThat(a+b, `is`(greaterThan(b)))
}
}
梯度依赖
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.51"
// ...
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-library:1.3'
// ...
}
其他系统信息
-
安卓工作室3.1.3
-
等级4.4
-
Android gradle插件3.1.3