1

我正在尝试为 Android 创建一个特征InstrumentationTestCase,其中包含一个抽象属性和一个使用该属性的方法。不幸的是,当我运行此测试时,它会以java.lang.VerifyError. 所以这是一个导致崩溃的代码示例:

trait ExtendedInstrumentationTestCase : InstrumentationTestCase {
    val string: String

    fun printString(): Unit {
        println(string)
    }
}

class MyApplicationTestCase :
        ApplicationTestCase<Application>(javaClass<Application>()),
        ExtendedInstrumentationTestCase {

    override val string: String = "test"

    override fun setUp() {
        super<ApplicationTestCase>.setUp()

        printString()
    }

    override fun tearDown() {
        super<ApplicationTestCase>.tearDown()
    }
}

这就是我从 logcat 中得到的:

W/dalvikvm﹕ VFY: Lcom/kotlintest/MyApplicationTestCase; is not instance of Landroid/test/InstrumentationTestCase;
W/dalvikvm﹕ VFY: bad arg 0 (into Landroid/test/InstrumentationTestCase;)
W/dalvikvm﹕ VFY:  rejecting call to Lcom/kotlintest/ExtendedInstrumentationTestCase$$TImpl;.printString (Landroid/test/InstrumentationTestCase;)V
W/dalvikvm﹕ VFY:  rejecting opcode 0x71 at 0x0000
W/dalvikvm﹕ VFY:  rejected Lcom/kotlintest/MyApplicationTestCase;.printString ()V
W/dalvikvm﹕ Verifier rejected class Lcom/kotlintest/MyApplicationTestCase;
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1e90648)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.VerifyError: com/kotlintest/MyApplicationTestCase
            at java.lang.Class.getDeclaredConstructors(Native Method)
            at java.lang.Class.getConstructors(Class.java:459)
            at android.test.suitebuilder.TestGrouping$TestCasePredicate.hasValidConstructor(TestGrouping.java:228)
            at android.test.suitebuilder.TestGrouping$TestCasePredicate.apply(TestGrouping.java:217)
            at android.test.suitebuilder.TestGrouping$TestCasePredicate.apply(TestGrouping.java:213)
            at android.test.suitebuilder.TestGrouping.select(TestGrouping.java:172)
            at android.test.suitebuilder.TestGrouping.selectTestClasses(TestGrouping.java:162)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

当我将该printString()方法设为扩展方法时,我的测试开始工作:

trait ExtendedInstrumentationTestCase : InstrumentationTestCase {
    val string: String
}

fun ExtendedInstrumentationTestCase.printString(): Unit {
    println(string)
}

据我所知,该错误在 Dalvik 中,但我想确切地知道错误在哪里,为什么会发生以及如何更改我的特征的第一个版本以使此代码正常工作。

4

1 回答 1

4

这是 Kotlin 编译器中的一个错误:KT-3006

问题是你继承了一个需要类的特征,而不调用超类的构造函数。编译器错误地允许这样做,并且在运行时稍后会爆炸。

正确的解决方法是显式调用InstrumentationTestCase的构造函数:

class MyApplicationTestCase :
        ApplicationTestCase<Application>(javaClass<Application>()),
        ExtendedInstrumentationTestCase,
        InstrumentationTestCase() {
...
于 2014-07-11T16:06:42.523 回答