我正在尝试为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中,但我想确切地知道错误的位置,为什么会发生错误,以及如何更改我的特性的第一个版本以使此代码正常工作。
答案 0 :(得分:4)
这是Kotlin编译器中的一个错误:KT-3006。
问题是你是否从一个需要一个类的特性继承而没有调用超类&#39;构造函数。编译器错误地允许这个,并且它会在运行时稍后爆炸。
正确的解决方法是明确调用InstrumentationTestCase
的构造函数:
class MyApplicationTestCase :
ApplicationTestCase<Application>(javaClass<Application>()),
ExtendedInstrumentationTestCase,
InstrumentationTestCase() {
...