This is my first time setting up an Android test project to test a Android project.
I've created a very basic test case which I'm trying to get to run, however what I have does not run. I get a Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
.
I would troubleshot this more, however I don't get any more information, such as which class it is trying to find, etc. Any ideas on how to get more information from the error? Any common areas I should look at, or anything that might need to be configured that I'm overlooking?
Below is the information from the console:
[2013-04-16 13:21:49 - XyzProgramTest] Android Launch!
[2013-04-16 13:21:49 - XyzProgramTest] adb is running normally.
[2013-04-16 13:21:49 - XyzProgramTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2013-04-16 13:21:49 - XyzProgramTest] Automatic Target Mode: launching new emulator with compatible AVD 'GalaxyNexusAPI_17'
[2013-04-16 13:21:49 - XyzProgramTest] Launching a new emulator with Virtual Device 'GalaxyNexusAPI_17'
[2013-04-16 13:21:53 - Emulator] extension WGL_ARB_make_current_read was not found
[2013-04-16 13:21:53 - Emulator] extension WGL_EXT_swap_control was not found
[2013-04-16 13:21:53 - Emulator] Failed to create pbuf surface for FB 0x3004
[2013-04-16 13:21:53 - Emulator] emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
[2013-04-16 13:21:54 - XyzProgramTest] New emulator found: emulator-5554
[2013-04-16 13:21:54 - XyzProgramTest] Waiting for HOME ('android.process.acore') to be launched...
[2013-04-16 13:22:55 - XyzProgramTest] HOME is up on device 'emulator-5554'
[2013-04-16 13:22:55 - XyzProgramTest] Uploading XyzProgramTest.apk onto device 'emulator-5554'
[2013-04-16 13:22:55 - XyzProgramTest] Installing XyzProgramTest.apk...
[2013-04-16 13:23:57 - XyzProgramTest] Success!
[2013-04-16 13:23:57 - XyzProgramTest] Project dependency found, installing: XyzProgram
[2013-04-16 13:23:57 - XyzProgram] Uploading XyzProgram.apk onto device 'emulator-5554'
[2013-04-16 13:23:58 - XyzProgram] Installing XyzProgram.apk...
[2013-04-16 13:24:05 - XyzProgram] Success!
[2013-04-16 13:24:05 - XyzProgramTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2013-04-16 13:24:07 - XyzProgramTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
Additional Notes
In my Android test project, I created a simple test with a package similar to the package that matches up with the class I'm trying to test in my Android project. So something like com.company.android.projectname
. I've specified this in the Android test project manifest
file's instrumentation
section.
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.company.android.projectname" />
When I changed this to match that, I get a new error that says Test run failed: Unable to find instrumentation target package: com.company.android.projectname
I'm not 100% sure what that targetPackage
should be and if I need multiple instrumentations for each package I want to test, etc. When I set it to com.company.android
that is when I get the java.lang.ClassNotFoundException
.
I had this problem after a merge, in which build.gradle was specifying the wrong test package, since I had been using the default.
By simply removing these lines from build.gradle, it was able to find the necessary classes in the default package, which is
src/androidTest
.This will probably not be the exact solution, but hopefully guides someone in the right direction.
In my case, I was trying to run the tests on Jelly Bean emulator. Android instrumentation does not seem to support Jelly Bean. I could run the tests on lollipop.
You may also not able to run android tests through release build type. Ensure that debug build type is active.
I've encountered this problem today, I adopted android-specific ant build.xml which sitting in "${sdk.dir}/tools/ant/" directory to perform all the test cases in command-line, but the STDOUT reports error to me just like yours:
I spend a few time to figuring out this, I found the main cause due in the root module's
AndroidManifest.xml
, I thought the application element is nothing to be configure so I omitted it in the manifest element, which causing my problem.I don't sure you share the same structure with me, so I post my project structure here, also the key file's content, hope can help somebody.
My project structure followed Android Test Projects's description which Google suggest us to do, consist of root library module which carrying core logic, and test module which inside the root module. Above are the key resources in project, but weren't all files, I shall post the project link here once I release it.
------------ Update 2014-01-06 --------------
Currently, my project was published on my github : aSQLitePlus-android, check the details if interested.
I had upgraded to androidx libraries and started getting this error.
To fix it, in build.gradle I changed the line:
to
Figured out my issue and am posting the answer for documentation purposes...
The root of my problem was 2 different things:
android.app.Application
class and my activities. This made it so my main program was not working as I needed to update my manifest in myandroid project
.android test project
to point to the updated location of the class that extendsandroid.app.Application
.After that, did some cleans and tested again and things were good.
FYI I ran into this and had to add
<uses-library android:name="android.test.runner"/>
within my
<application/>
tag