Why am I getting a IllegalAccessError when trying to run my test project with code coverage?
I get the following error when running 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
I have a class in my App
public class MyClass {
public boolean myMethod(int i) {
return true;
}
}
And a test class in my Test App
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...
}
}
I have created my two ant build.xml with the following parameters
host:MyApp mach$ android update project --path $PWD --name MyApp --target android-16 --subprojects
host:MyAppTest mach$ android update test-project --main ../MyApp --path ./
Found the answer - I tried to instrument the unit test as well.
So here is the short version of successfully building a Unit test and getting code coverage
In Eclipse, for your project, open Properties->Java Build Path and select the "Libraries"-tab. Press "Add External JARs..." and select emma.jar form your SDK.([...]/sdk/tools/lib/emma.jar)
Select the "Order and Export" tab and select to export emma.jar.
Open a terminal and change directory to the root of your project-to-be-tested.
android update project --path $PWD --name [YOUR PROJECT NAME] --target android-17 --subprojects
Create a test project.
android create test-project -m ../ -n MyAppTest -p tests
Write your test cases in the new test project
When it is time to test first build a instrumented build of the project-to-be-tested and then build and execute the tests
ant clean instrument
cd tests
ant debug emma install test
You will find the coverage report in tests/bin
Cheers!