Why does this error occur when I try to run a Junit test-case on the Android emulator?

After following some instructions on Diego Torres blog I am able to test my classes using the regular Eclipse JUnit test-runner, however I also want to be able to run my unit tests from within the emulated android environment.

For now this should make no real difference since the classes I am working on do very basic numerical or networking tasks, but obviously anything which involves the UI or Android API specific functions can only ever be tested on the emulator.

I set up a new run configuration of type AndroidJUnitTest wich uses the android.test.InstrumentationTestRunner, however whenever I fire up the test I get the following error in the console:

[2010-01-09 00:45:23 - Pal1]Uploading Pal1.apk onto device 'emulator-5556'
[2010-01-09 00:45:23 - Pal1]Installing Pal1.apk...
[2010-01-09 00:45:25 - Pal1]Success!
[2010-01-09 00:45:25 - Pal1]Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5556
[2010-01-09 00:45:27 - Pal1]Test run failed: Unable to instantiate instrumentation ComponentInfo{org.stodge.traderpal/android.test.InstrumentationTestRunner}: java.lang.ClassNotFoundException: android.test.InstrumentationTestRunner in loader dalvik.system.PathClassLoader@43598c28

As you might expect, the test ends (without actually having run any tests after the final "Unable to instantiate instrumentation ComponentInfo" message.

For now my test-cases consist of nothing more than Junit3 and Junit4 boiler-plater code. The implementations of the tests consist of nothing more than fail() calls. All I need to do at this stage is show that I can execute a basic unit-test in both environments so that I can begin test-driven development.

Notes: - Running Eclipse 3.5 on Ubuntu 64bit - Emulator is set to API level 2 - I'm new to both Android and Java, so excuse my basic questions

Asked by: John809 | Posted: 20-01-2022

Answer 1

I would ignore what that blog says; it seems pretty outdated.

You don't need to import JUnit libraries or anything like that; the appropriate classes, TestCase and so on are integrated into the Android SDK.

Just create a new class in your project and extend AndroidTestCase. Then you can right click the test case (or package of test cases) you want to run and choose Run As → Android JUnit Test.

Also, I would use an API level higher than 2. Virtually no devices in use are running Android 1.1; you can safely switch to API level 3, which is Android 1.5 and is a big improvement over previous versions.

Answered by: Charlie614 | Posted: 21-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android