Need help in running the android junit test case for andriod APIDemo in eclipse

841 views
Skip to first unread message

yin...@gmail.com

unread,
Jan 28, 2009, 7:55:30 PM1/28/09
to Android Developers
Hi,

I am trying to run Junit test case for the android APIDemo project
under eclipse.

I create an eclipse project from the APIDemo source, it compiles fine
and then I did:
1. Debug->Run as Junit tests

But I get this error:
'Lanuching AllTests' has encountered a problem.
Cannot connect to VM.

And I find this in the .log file in eclipse:
!ENTRY org.eclipse.core.net 1 0 2009-01-28 15:03:52.799
!MESSAGE System property http.nonProxyHosts has been set to
local|*.local|169.25
4/16|*.169.254/16 by an external source. This value will be
overwritten
using th
e values from the preferences

!ENTRY org.eclipse.jdt.launching 4 120 2009-01-28 15:04:03.269
!MESSAGE Cannot connect to VM
!STACK 0
java.net.SocketException: Socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
at java.net.ServerSocket.implAccept(ServerSocket.java:450)
at java.net.ServerSocket.accept(ServerSocket.java:421)
at
org.eclipse.jdi.internal.connect.SocketTransportService.accept(Socket
TransportService.java:95)
at
org.eclipse.jdi.internal.connect.SocketTransportImpl.accept(SocketTra
nsportImpl.java:56)
at
org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl.accept(
SocketListeningConnectorImpl.java:135)
at
org.eclipse.jdt.internal.launching.StandardVMDebugger$ConnectRunnable
run(StandardVMDebugger.java:107)
at java.lang.Thread.run(Thread.java:613)

!ENTRY org.eclipse.jdt.launching 4 120 2009-01-28 15:23:53.932
!MESSAGE Cannot connect to VM
!STACK 0
java.net.SocketException: Socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
at java.net.ServerSocket.implAccept(ServerSocket.java:450)
at java.net.ServerSocket.accept(ServerSocket.java:421)
at
org.eclipse.jdi.internal.connect.SocketTransportService.accept(Socket
TransportService.java:95)
at
org.eclipse.jdi.internal.connect.SocketTransportImpl.accept(SocketTra
nsportImpl.java:56)
at
org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl.accept(
SocketListeningConnectorImpl.java:135)
at
org.eclipse.jdt.internal.launching.StandardVMDebugger$ConnectRunnable
run(StandardVMDebugger.java:107)
at java.lang.Thread.run(Thread.java:613)


Thank you for any help.


Diego Torres Milano

unread,
Jan 29, 2009, 6:08:38 PM1/29/09
to Android Developers

yin...@gmail.com

unread,
Jan 29, 2009, 8:44:46 PM1/29/09
to Android Developers
Thanks. But can you please tell me if it is possible to
* run junit test within eclipse using the 'Debug->Run as Junit Test'?
* run junit test as part of the ant script? (i.e. automatically via
command line)

Thank you.


On Jan 29, 3:08 pm, Diego Torres Milano <dtmil...@gmail.com> wrote:
> This may help youhttp://dtmilano.blogspot.com/2008/11/android-testing-on-android-platf...

Xavier Ducrohet

unread,
Jan 29, 2009, 9:12:35 PM1/29/09
to android-d...@googlegroups.com
You will never really be able to just use the default Run as JUnit test action.

This is because this setups up the tests to run using the system
library of the project which, for Android project, is android.jar.
android.jar in the SDK is only stubbed methods/classes, and contains no code.

You can run unit tests by setting up Eclipse using the follow
instructions: http://code.google.com/android/kb/troubleshooting.html#addjunit

Note that this will only work for tests not using the Android APIs.

If you wish to run true android test, please use instrumentations
running on the device.
See http://code.google.com/android/reference/android/test/InstrumentationTestRunner.html
for more information.

The information from Diego to setup the test runners for ApiDemos is
good, except that you don't need to create a new project and copy
things over, you can simply create a project for the existing source
(you will need to link the apidemos project in your build path).

Xav

ying lcs

unread,
Jan 29, 2009, 9:25:32 PM1/29/09
to android-d...@googlegroups.com
On Thu, Jan 29, 2009 at 6:12 PM, Xavier Ducrohet <x...@google.com> wrote:
>
> You will never really be able to just use the default Run as JUnit test action.
>
> This is because this setups up the tests to run using the system
> library of the project which, for Android project, is android.jar.
> android.jar in the SDK is only stubbed methods/classes, and contains no code.
>
> You can run unit tests by setting up Eclipse using the follow
> instructions: http://code.google.com/android/kb/troubleshooting.html#addjunit
>
> Note that this will only work for tests not using the Android APIs.
>
Thanks. But most likely most of my classes will use android APIs, so
using this to unit-test is not that common, right?
Or I am mis-understanding the condition.
I would like to have 1 way to unit-test my android code.


> If you wish to run true android test, please use instrumentations
> running on the device.
> See http://code.google.com/android/reference/android/test/InstrumentationTestRunner.html
> for more information.
>



> The information from Diego to setup the test runners for ApiDemos is
> good, except that you don't need to create a new project and copy
> things over, you can simply create a project for the existing source
> (you will need to link the apidemos project in your build path).
>

Thank you. I have followed Diego instructions, and I finally get that to work.
But you mention that there a way not to create a new project.
Can you please tell me how to do that?

I have create an 'apidemos' project in eclipse, but the 'tests'
directory is not in the build path. Hence they are not compile.

So what should I do to run the code 'tests' code without creating a
new project like Diego described.

Thank you for any tips.

Xavier Ducrohet

unread,
Jan 29, 2009, 9:31:34 PM1/29/09
to android-d...@googlegroups.com
Using the "new android project" wizard, you can create a project from
existing source.
This is probably what you used to create a project for ApiDemos. Just
use it again for the tests project (point the wizard to
<sdk>/samples/ApiDemos/tests).

The compilation problem is because the tests project doesn't know the
ApiDemos classes until you tell it where they are.
Right click the tests project in the package Explorer and choose Build
Path > Configure Build Path.
In the "Projects" tab, click Add and choose your ApiDemos project.

Because deployment of instrumentation projects is not supported in ADT
you will have install ApiDemos and your test project manually.

We do intend to improve this workflow in the future.

Xav

ying lcs

unread,
Jan 30, 2009, 3:17:32 PM1/30/09
to android-d...@googlegroups.com
Thank you. Xavier,

Can you please tell me if this will run other vanilla junit test
cases? The ones just inherit from Junit framework, not the ones which
are sub-classed from Google TestCase classes?

Thank you.

ying lcs

unread,
Jan 30, 2009, 5:21:08 PM1/30/09
to android-d...@googlegroups.com
Hi,

I tried that again, but it does not work now, for some reason.

I did load 'ApiDemos' project first, but I don't see the 'Test for Api
Demo' in the 'Instrumentation.' screen.
Then, I manually load the 'ApiDemosTest2' project again, I still don't
see the 'Test for Api Demo'.

But from the Eclipse output, I do see that both apk has been installed.

Any idea why it does not work?
Thank you for any help.

[2009-01-30 14:09:26 - ApiDemos] HOME is up on device 'emulator-5554'
[2009-01-30 14:09:26 - ApiDemos] Uploading ApiDemos.apk onto device
'emulator-5554'
[2009-01-30 14:09:28 - ApiDemos] Installing ApiDemos.apk...
[2009-01-30 14:09:37 - ApiDemos] Success!
[2009-01-30 14:09:38 - ApiDemos] Starting activity
com.example.android.apis.ApiDemos on device
[2009-01-30 14:09:39 - ApiDemos] ActivityManager: Starting: Intent {
comp={com.example.android.apis/com.example.android.apis.ApiDemos} }
[2009-01-30 14:09:39 - ApiDemos] Attempting to connect debugger to
'com.example.android.apis' on port 8613
[2009-01-30 14:15:00 - ApiDemosTest2] ------------------------------
[2009-01-30 14:15:00 - ApiDemosTest2] Android Launch!
[2009-01-30 14:15:00 - ApiDemosTest2] adb is running normally.
[2009-01-30 14:15:00 - ApiDemosTest2] Launching:
com.example.android.apis.tests.Dummy
[2009-01-30 14:15:00 - ApiDemosTest2] Automatic Target Mode: using
existing emulator: emulator-5554
[2009-01-30 14:15:00 - ApiDemosTest2] Uploading ApiDemosTest2.apk onto
device 'emulator-5554'
[2009-01-30 14:15:00 - ApiDemosTest2] Installing ApiDemosTest2.apk...
[2009-01-30 14:15:03 - ApiDemosTest2] Success!
[2009-01-30 14:15:03 - ApiDemosTest2] Starting activity
com.example.android.apis.tests.Dummy on device
[2009-01-30 14:15:04 - ApiDemosTest2] ActivityManager: Starting:
Intent { comp={com.example.android.apis.tests/com.example.android.apis.tests.Dummy}
}
[2009-01-30 14:15:04 - ApiDemosTest2] Attempting to connect debugger
to 'com.example.android.apis.tests' on port 8617

gudujarlson

unread,
Mar 23, 2009, 11:16:48 PM3/23/09
to Android Developers
I have been struggling with this topic for a couple days. I am using
Android 1.0R2, Eclipse 3.4.2, and Windows XP SP3.

I can create the ApiDemos project just fine and it compiles as well,
but I fail at creating and building the ApiDemosTest project. Here is
what I do.

1) Open Eclipse.
2) Select File -> New Android Project
3) Select Create project from existing source
4) Enter a location of C:\code\android-sdk-windows-1.0_r2\samples
\ApiDemos\tests
5) Enter a project name of ApiDemosTest

I receive the error "No activity name defined in C:\code\android-sdk-
windows-1.0_r2\samples\ApiDemos\tests\AndroidManifest.xml" and the
Finish button is greyed out.

Ok, so now lets try Diego's method.

1) Open Eclipse
2) Select File -> New Android Project
3) Enter 'ApiDemosTest" for the project name
4) Enter "com.example.android.apis.tests" for the package.
5) Enter "Dummy" for the activity name.
6) Enter "Dummy" for the application name.
7) Click on the Finish button.
8) In the Navigator, select ApiDemos/tests and drag it into
ApiDemosTest.
9) Right click on ApiDemosTest and select Properties.
10) Select Java Build Path.
11) Select the Source tab.
12) Select ApiDemosTest/src and click on the Remove button.
13) Click on the Add Folder button.
14) Select tests/src.
15) Select the Projects tab.
16) Click on the Add button.
17) Select ApiDemos.
18) Click on the Ok button
19) Click on the Ok button
20) Select Project -> Build All.

The result is that I get the following errors:

Description Resource Path Location Type
Class com.example.android.apis.tests.Dummy does not exist
AndroidManifest.xml ApiDemosTest line 8 Android Problem

Description Resource Path Location Type
Package 'com.example.android.apis.tests' does not exist!
AndroidManifest.xml ApiDemosTest Unknown aapt Problem

Help?
Reply all
Reply to author
Forward
0 new messages