运行Android jUnit Test时出现java.lang.VerifyError

时间:2011-05-16 22:59:41

标签: android junit

我一直想弄清楚为什么我会继续java.lang.VerifyError 每次我尝试在我的任何jUnit测试中实例化我的一个类,但是我完全被难倒了。

我创建了一个简单的,最小的测试用例,展示了这种行为:

package com.example;

public class Foo {

    private int bar;

    public Foo(int b) {
        bar = b;
    }

    public int getBar() {
        return bar;
    }

}

单元测试:

package com.example.tests;

import com.example.Foo;
import junit.framework.TestCase;

public class FooTest extends TestCase {

    private Foo foo;

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testSanity() {
        foo = new Foo(1);
        assertTrue(true);
    }
}

当单元测试尝试实例化Foo对象时,例如testSanity方法的第一行,我得到java.lang.VerifyError

该项目正在针对Android 1.6构建。 Eclipse中DDMS的堆栈跟踪如下所示:

05-16 23:40:01.543: ERROR/AndroidRuntime(934): Uncaught handler: thread main exiting due to uncaught exception
05-16 23:40:01.563: ERROR/AndroidRuntime(934): java.lang.VerifyError: com.example.tests.FooTest
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at java.lang.Class.getDeclaredConstructors(Native Method)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at java.lang.Class.getConstructor(Class.java:484)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at junit.framework.TestSuite.getTestConstructor(TestSuite.java:177)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at junit.framework.TestSuite.<init>(TestSuite.java:59)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:103)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:124)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:52)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:81)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:375)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:357)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:325)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3848)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.app.ActivityThread.access$2800(ActivityThread.java:116)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.os.Looper.loop(Looper.java:123)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at android.app.ActivityThread.main(ActivityThread.java:4203)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at java.lang.reflect.Method.invoke(Method.java:521)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
05-16 23:40:01.563: ERROR/AndroidRuntime(934):     at dalvik.system.NativeStart.main(Native Met

任何帮助解决这个问题并让测试运行都会非常感激。

更新

fadden's回答之后,VFY logcat输出如下:

05-18 20:33:04.052: ERROR/dalvikvm(234): Could not find class 'com.example.Foo', referenced from method com.example.tests.FooTest.testSanity
05-18 20:33:04.052: WARN/dalvikvm(234): VFY: unable to resolve new-instance 5 (Lcom/example/Foo;) in Lcom/example/tests/FooTest;
05-18 20:33:04.052: WARN/dalvikvm(234): VFY:  rejecting opcode 0x22 at 0x0001
05-18 20:33:04.052: WARN/dalvikvm(234): VFY:  rejected Lcom/example/tests/FooTest;.testSanity ()V
05-18 20:33:04.052: WARN/dalvikvm(234): Verifier rejected class Lcom/example/tests/FooTest;

1 个答案:

答案 0 :(得分:3)

1.5 / 1.6的验证者非常严厉。问题的详细解释是在异常之上的行上(在logcat输出中查找“VFY”)。

UPDATE:查看logcat输出,com.example.tests.FooTest中的代码无法找到com.example.Foo。在Android&gt; = 2.0中,这将导致ClassNotFoundException,但在1.6中,验证程序变得暴乱,只是拒绝整个类。

所以问题是:为什么不能找到它?我要检查的第一件事是确保com.example.Foo和com.example.tests.FooTest都出现在相同的APK / classes.dex文件中。您可以通过“dexdump”检查它来检查这一点。