Android JUnit 测试因 java.lang.VerifyError 失败
阿罗哈,
我一直遵循这里的指南:
http://developer.android。 com/resources/tutorials/testing/helloandroid_test.html
为新的 Android 项目创建一些简单的测试用例。前几个测试用例运行良好,但现在我无法运行任何测试。这是我得到的输出的示例:
[2011-03-25 10:05:01 - application-tests] Android Launch!
[2011-03-25 10:05:01 - application-tests] adb is running normally.
[2011-03-25 10:05:01 - application-tests] Performing android.test.InstrumentationTestRunner JUnit launch
[2011-03-25 10:05:01 - application-tests] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'GalaxyTabRunning2.3'
[2011-03-25 10:05:02 - application-tests] Application already deployed. No need to reinstall.
[2011-03-25 10:05:02 - application-tests] Project dependency found, installing: application
[2011-03-25 10:05:03 - application] Application already deployed. No need to reinstall.
[2011-03-25 10:05:03 - application-tests] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-03-25 10:05:03 - application-tests] Collecting test information
[2011-03-25 10:05:06 - application-tests] Test run failed: java.lang.VerifyError
测试用例非常简单:
/**
* Test case to test a null input parameter to the decode method.
*
*/
public void testNullInputPerformCrcDecoding()
{
try
{
AppProtocolDecoder.performCrcDecoding(null);
fail("Expected IllegalArgumentException to be thrown...");
}
catch (IllegalArgumentException expected)
{
assertTrue(expected instanceof IllegalArgumentException);
}
catch (ProtocolException ve)
{
fail("Unexpected VisiProtocolException occured: " + ve);
}
}
AppProtocolDecoder.performCrcDecoding(null);
是一个静态方法。我已经删除了其中的所有代码,因此它所做的只是返回 null。测试总是失败并出现 java.lang.VerifyError。
这是在 Android 2.3 下使用 Eclipse 中的 Android JUnit 测试运行器进行的。
有什么想法吗?我尝试重新创建测试项目和各种代码修改排列。
提前致谢!
编辑(!):Logcat 输出:
D/AndroidRuntime( 919): Shutting down VM
W/dalvikvm( 919): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 919): FATAL EXCEPTION: main
E/AndroidRuntime( 919): java.lang.VerifyError: com.company.h1s.androidserver.test.protocol.AppProtocolDecoderTest
E/AndroidRuntime( 919): at java.lang.Class.getDeclaredConstructors(Native Method)
E/AndroidRuntime( 919): at java.lang.Class.getConstructors(Class.java:490)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping$TestCasePredicate.hasValidConstructor(TestGrouping.java:226)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping$TestCasePredicate.apply(TestGrouping.java:215)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping$TestCasePredicate.apply(TestGrouping.java:211)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.select(TestGrouping.java:170)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.selectTestClasses(TestGrouping.java:160)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:154)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:115)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:103)
E/AndroidRuntime( 919): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:360)
E/AndroidRuntime( 919): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3210)
E/AndroidRuntime( 919): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
E/AndroidRuntime( 919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:966)
E/AndroidRuntime( 919): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 919): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 919): at android.app.ActivityThread.main(ActivityThread.java:3647)
E/AndroidRuntime( 919): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 919): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 919): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 61): Error in app com.company.h1s.androidserver running instrumentation ComponentInfo{com.company.h1s.androidserver.test/android.test.InstrumentationTestRunner}:
W/ActivityManager( 61): java.lang.VerifyError
W/ActivityManager( 61): java.lang.VerifyError: com.company.h1s.androidserver.test.protocol.AppProtocolDecoderTest
I/ActivityManager( 61): Force stopping package com.company.h1s.androidserver uid=10031
I/Process ( 61): Sending signal. PID: 919 SIG: 9
D/AndroidRuntime( 911): Shutting down VM
我正在使用 Maven 来实现类依赖项。以下是我通过 Maven 包含的库:
/.m2/repository/com/google/android/android/2.2.1/android-2.2.1.jar
/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
/.m2/repository/org/apache/httpcomponents/httpclient/4.0.1/httpclient-4.0.1.jar
/.m2/repository/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar
/.m2/repository/commons-codec/commons-codec/1.3/commons-codec-1.3.jar
/.m2/repository/org/khronos/opengl-api/gl1.1-android-2.1_r1/opengl-api-gl1.1-android-2.1_r1.jar
/.m2/repository/xerces/xmlParserAPIs/2.6.2/xmlParserAPIs-2.6.2.jar
/.m2/repository/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar
/.m2/repository/org/json/json/20080701/json-20080701.jar
/.m2/repository/org/slf4j/slf4j-android/1.6.1-RC1/slf4j-android-1.6.1-RC1.jar
/.m2/repository/org/slf4j/log4j-over-slf4j/1.6.1/log4j-over-slf4j-1.6.1.jar
/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar
/.m2/repository/org/apache/mina/mina-core/2.0.0/mina-core-2.0.0.jar
/.m2/repository/com/company/app-common/1.0-SNAPSHOT/app-common-1.0-SNAPSHOT.jar
/.m2/repository/commons-lang/commons-lang/2.4/commons-lang-2.4.jar
/.m2/repository/com/company/app-data/1.0-SNAPSHOT/app-data-1.0-SNAPSHOT.jar
/.m2/repository/org/springframework/spring/2.5.6/spring-2.5.6.jar
Aloha,
I've been following the guidelines here:
http://developer.android.com/resources/tutorials/testing/helloandroid_test.html
To create some simple test cases for a new Android project. The first few test cases were working fine, but now I am not able to get any tests to run. Here is an example of the output I am getting:
[2011-03-25 10:05:01 - application-tests] Android Launch!
[2011-03-25 10:05:01 - application-tests] adb is running normally.
[2011-03-25 10:05:01 - application-tests] Performing android.test.InstrumentationTestRunner JUnit launch
[2011-03-25 10:05:01 - application-tests] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'GalaxyTabRunning2.3'
[2011-03-25 10:05:02 - application-tests] Application already deployed. No need to reinstall.
[2011-03-25 10:05:02 - application-tests] Project dependency found, installing: application
[2011-03-25 10:05:03 - application] Application already deployed. No need to reinstall.
[2011-03-25 10:05:03 - application-tests] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-03-25 10:05:03 - application-tests] Collecting test information
[2011-03-25 10:05:06 - application-tests] Test run failed: java.lang.VerifyError
The test case is very simple:
/**
* Test case to test a null input parameter to the decode method.
*
*/
public void testNullInputPerformCrcDecoding()
{
try
{
AppProtocolDecoder.performCrcDecoding(null);
fail("Expected IllegalArgumentException to be thrown...");
}
catch (IllegalArgumentException expected)
{
assertTrue(expected instanceof IllegalArgumentException);
}
catch (ProtocolException ve)
{
fail("Unexpected VisiProtocolException occured: " + ve);
}
}
AppProtocolDecoder.performCrcDecoding(null);
is a static method. I've removed all the code from it so all it does, literally is return null. The test always fails with the java.lang.VerifyError.
This is under Android 2.3 using the Android JUnit Test runner in Eclipse.
Any ideas? I've tried recreating the test project and all sorts of permutations of code modifications.
Thanks in advance!
EDIT (!): Logcat output:
D/AndroidRuntime( 919): Shutting down VM
W/dalvikvm( 919): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 919): FATAL EXCEPTION: main
E/AndroidRuntime( 919): java.lang.VerifyError: com.company.h1s.androidserver.test.protocol.AppProtocolDecoderTest
E/AndroidRuntime( 919): at java.lang.Class.getDeclaredConstructors(Native Method)
E/AndroidRuntime( 919): at java.lang.Class.getConstructors(Class.java:490)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping$TestCasePredicate.hasValidConstructor(TestGrouping.java:226)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping$TestCasePredicate.apply(TestGrouping.java:215)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping$TestCasePredicate.apply(TestGrouping.java:211)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.select(TestGrouping.java:170)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.selectTestClasses(TestGrouping.java:160)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:154)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:115)
E/AndroidRuntime( 919): at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:103)
E/AndroidRuntime( 919): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:360)
E/AndroidRuntime( 919): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3210)
E/AndroidRuntime( 919): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
E/AndroidRuntime( 919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:966)
E/AndroidRuntime( 919): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 919): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 919): at android.app.ActivityThread.main(ActivityThread.java:3647)
E/AndroidRuntime( 919): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 919): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 919): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 61): Error in app com.company.h1s.androidserver running instrumentation ComponentInfo{com.company.h1s.androidserver.test/android.test.InstrumentationTestRunner}:
W/ActivityManager( 61): java.lang.VerifyError
W/ActivityManager( 61): java.lang.VerifyError: com.company.h1s.androidserver.test.protocol.AppProtocolDecoderTest
I/ActivityManager( 61): Force stopping package com.company.h1s.androidserver uid=10031
I/Process ( 61): Sending signal. PID: 919 SIG: 9
D/AndroidRuntime( 911): Shutting down VM
I am using Maven for class dependencies. Here are the libraries I am including via Maven:
/.m2/repository/com/google/android/android/2.2.1/android-2.2.1.jar
/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
/.m2/repository/org/apache/httpcomponents/httpclient/4.0.1/httpclient-4.0.1.jar
/.m2/repository/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar
/.m2/repository/commons-codec/commons-codec/1.3/commons-codec-1.3.jar
/.m2/repository/org/khronos/opengl-api/gl1.1-android-2.1_r1/opengl-api-gl1.1-android-2.1_r1.jar
/.m2/repository/xerces/xmlParserAPIs/2.6.2/xmlParserAPIs-2.6.2.jar
/.m2/repository/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar
/.m2/repository/org/json/json/20080701/json-20080701.jar
/.m2/repository/org/slf4j/slf4j-android/1.6.1-RC1/slf4j-android-1.6.1-RC1.jar
/.m2/repository/org/slf4j/log4j-over-slf4j/1.6.1/log4j-over-slf4j-1.6.1.jar
/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar
/.m2/repository/org/apache/mina/mina-core/2.0.0/mina-core-2.0.0.jar
/.m2/repository/com/company/app-common/1.0-SNAPSHOT/app-common-1.0-SNAPSHOT.jar
/.m2/repository/commons-lang/commons-lang/2.4/commons-lang-2.4.jar
/.m2/repository/com/company/app-data/1.0-SNAPSHOT/app-data-1.0-SNAPSHOT.jar
/.m2/repository/org/springframework/spring/2.5.6/spring-2.5.6.jar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你使用任何外部罐子吗?根据我的经验,这似乎是 java.lang.VerifyError 的常见来源。
正如这个答案建议您可能需要重新编译它们或通过 dx 运行它们,所以它们包含 dalvik 字节码。
编辑 - 我自己没有处理过这个问题,我不确定maven的方式来做到这一点,但在某些时候我认为你需要使用
dx
在你的 jar 上:如果可能的话,你可以尝试在你的 jar 上手动执行此操作,看看这是否确实是问题所在,然后尝试将其集成到你的 Maven 构建中。
Are you using any external jars? This seems to be a common source of java.lang.VerifyError in my experience.
As this answer suggests you may need to recompile them or run them through dx so that they contain dalvik bytecode.
Edit - I haven't dealt with this myself, and I'm not sure about the maven way to do this, but at some point I think you need to use
dx
on your jars:If possible you might try doing this manually on your jars to see if that's actually the problem, and then try to integrate that into your maven build afterwards.
我最终所做的只是删除使用这些库的代码。在大多数情况下,这样做是非常简单的。
What I ended up doing is simply removing the code that was using these libraries. In most cases it was pretty trivial to do so.
如果您在主项目中使用外部库,则应该将它们导出到您的测试中。
If you are using external libraries in your main project you should export them to your tests.
将我的构建工具从
19.1.0
升级到21.1.2
通过java.lang.VerifyError
修复了我的测试失败Upgrading my build tool to
21.1.2
from19.1.0
fixed my Tests failures withjava.lang.VerifyError