为什么在尝试运行具有代码覆盖率的测试项目时会收到 IllegalAccessError?
运行 ant 时出现以下错误。
host:MyAppTest mach$ ant clean emma instrument install test
[...]
[echo] Running tests ...
[exec]
[exec] com.example.myapp.test.MyClassTest:.
[exec] Error in testMyMethod:
[exec] java.lang.ExceptionInInitializerError
[exec] at com.example.myapp.test.MyClassTest.testMyMethod(MyClassTest.java:10)
[exec] at java.lang.reflect.Method.invokeNative(Native Method)
[exec] at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
[exec] at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
[exec] at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
[exec] at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
[exec] Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
[exec] at com.example.myapp.MyClass.$VRi(MyClass.java)
[exec] at com.example.myapp.MyClass.<clinit>(MyClass.java)
[exec] ... 13 more
我的应用程序中有一个课程
public class MyClass {
public boolean myMethod(int i) {
return true;
}
}
和我的测试应用程序中的一个测试类
public class MyClassTest extends AndroidTestCase {
public void testMyMethod() {
MyClass a = new MyClass(); // <--- THIS MAKES THE TEST FAIL
// If I remove this line it runs
// successfully but does not test anything...
}
}
我用以下参数创建了我的两个 ant build.xml
host:MyApp mach$ android update project --path $PWD --name MyApp --target android-16 --subprojects
host:MyAppTest mach$ android update test-project --main ../MyApp --path ./