簡體   English   中英

在Android應用中使用EMMA運行單元測試時出現IllegalAccessError

[英]IllegalAccessError when running Unit Test with EMMA for Android app

嘗試在代碼覆蓋范圍內運行測試項目時,為什么會收到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...
    }
}

我用以下參數創建了兩個螞蟻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 ./

找到了答案-我也嘗試了儀器測試。

所以這是成功構建單元測試並獲得代碼覆蓋的簡短版本

在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

您可以在測試/箱中找到覆蓋率報告

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM