【问题标题】:IllegalAccessError when running Unit Test with EMMA for Android app使用 EMMA for Android 应用程序运行单元测试时出现 IllegalAccessError
【发布时间】:2013-06-27 13:54:45
【问题描述】:

为什么我在尝试运行具有代码覆盖率的测试项目时会收到​​ 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 ./

【问题讨论】:

    标签: android unit-testing emma


    【解决方案1】:

    找到答案 - 我也尝试对单元测试进行检测。

    这里是成功构建单元测试并获得代码覆盖率的简短版本

    在 Eclipse 中,为您的项目打开 Properties->Java Build Path 并选择“Libraries”-选项卡。按“添加外部 JAR...”并从您的 SDK 中选择 emma.jar。([...]/sdk/tools/lib/emma.jar)

    选择“订购和导出”选项卡并选择导出 emma.jar。

    打开一个终端并将目录更改为要测试的项目的根目录。

    android update project --path $PWD --name [YOUR PROJECT NAME] --target android-17 --subprojects
    

    创建一个测试项目。

    android create test-project -m ../ -n MyAppTest -p tests
    

    在新的测试项目中编写你的测试用例

    何时需要测试,首先构建待测试项目的检测版本,然后构建并执行测试

    ant clean instrument
    cd tests
    ant debug emma install test
    

    你会在 tests/bin 中找到覆盖率报告

    干杯!

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      相关资源
      最近更新 更多