我对Kotlin有点熟悉,我想向它介绍另一个Android项目,作为测试的第一步。我决定直接从斯派克开始。
我将以下依赖项添加到待测试模块的build.gradle中:
testCompile 'junit:junit:4.12'
testCompile "org.jetbrains.kotlin:kotlin-stdlib:1.0.2"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.0.2"
testCompile "org.jetbrains.spek:spek:1.0.25"除其他外,我还使用了git的规范样例的SimpleTest类:
import org.jetbrains.spek.api.Spek
import kotlin.test.assertEquals
class SampleCalculator
{
fun sum(x: Int, y: Int) = x + y
fun subtract(x: Int, y: Int) = x - y
}
class SimpleTest : Spek({
describe("a calculator") {
val calculator = SampleCalculator()
it("should return the result of adding the first number to the second number") {
val sum = calculator.sum(2, 4)
assertEquals(6, sum)
}
it("should return the result of subtracting the second number from the first number") {
val subtract = calculator.subtract(4, 2)
assertEquals(2, subtract)
}
}
})代码编译,在Android中,在类声明旁边显示了一个绿色播放按钮来运行类的测试。然而,这样做会导致
Process finished with exit code 1
Class not found: "com.anfema.ionclient.serialization.SimpleTest"Empty test suite.我创建了JUnit3和JUnit4测试,它们都运行时没有任何问题。
我是否错过了Spek测试的其他配置步骤?
发布于 2016-05-23 15:01:02
我的失败是我没有完全/正确地配置Kotlin。
我错过了应用Kotlin插件,它是用以下行完成的:
apply plugin: 'kotlin-android'
buildscript {
ext.kotlin_version = '1.0.2'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}如果要在普通JUnit测试中使用Kotlin代码,这也是必需的。
https://stackoverflow.com/questions/37392478
复制相似问题