2013-06-27 37 views
0

爲什麼在嘗試運行帶代碼覆蓋率的測試項目時遇到IllegalAccessError?使用EMMA for Android應用程序運行單元測試時發生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 ./ 

回答

0

找到答案r - 我也嘗試過單元測試。

所以這裏的成功構建單元測試和獲取代碼覆蓋率

在Eclipse中,爲您的項目,打開屬性 - > Java構建路徑短版本,然後選擇「庫」 -Tab。 ([...]/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 

您會在測試/ bin中找到覆蓋率報告

乾杯!

相關問題