trouble separating test code from shipping code

32 views
Skip to first unread message

Brian Cavagnolo

unread,
Nov 5, 2009, 8:30:27 PM11/5/09
to android-...@googlegroups.com
Hello,

I've got a project laid out like so:

|-- Android.mk
|-- AndroidManifest.xml
`-- java
`-- com
`-- foo
|-- Foo.java
`-- FooTest.java

Foo is a simple class. It does not extend Activity or Application.
FooTest.java extends TestCase class. I successfully run the tests
like so:

$ adb shell am instrument -w com.foo/android.test.InstrumentationTestRunner

Test results for InstrumentationTestRunner=
Time: 0.001

OK (0 tests)

Hooray! Now I want to move FooTest.java into a tests/ directory so
the tree looks like this:

|-- Android.mk
|-- AndroidManifest.xml
|-- java
| `-- com
| `-- foo
| `-- Foo.java
`-- tests
|-- Android.mk
|-- AndroidManifest.xml
`-- java
`-- com
`-- foo
`-- FooTest.java

Everything builds fine, but I get a runtime error when I try to run the tests:

adb shell am instrument -w com.foo.tests/android.test.InstrumentationTestRunner
INSTRUMENTATION_RESULT: shortMsg=Unable to instantiate instrumentation
ComponentInfo{com.foo.tests/android.test.InstrumentationTestRunner}:
java.lang.ClassNotFoundException:
android.test.InstrumentationTestRunner in loader
dalvik.system.PathClassLoader@4001ada0
INSTRUMENTATION_RESULT: longMsg=java.lang.RuntimeException: Unable to
instantiate instrumentation
ComponentInfo{com.foo.tests/android.test.InstrumentationTestRunner}:
java.lang.ClassNotFoundException:
android.test.InstrumentationTestRunner in loader
dalvik.system.PathClassLoader@4001ada0
INSTRUMENTATION_CODE: 0

I'm pretty sure I'm missing something simple. Below are the
Android.mk and AndroidManifest.xml files from the failing source tree.
Please help!

$ cat Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng user
LOCAL_SRC_FILES := $(call all-java-files-under, java)
LOCAL_PACKAGE_NAME := Foo
include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))

$ cat AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo">
</manifest>

$ cat tests/Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := tests
LOCAL_JAVA_LIBRARIES := android.test.runner
LOCAL_SRC_FILES := $(call all-java-files-under, java)
LOCAL_PACKAGE_NAME := FooTests
LOCAL_INSTRUMENTATION_FOR := Foo
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)

$ cat tests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo.tests">
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.foo"
android:label="Foo Tests."/>
</manifest>

Brian Cavagnolo

unread,
Nov 5, 2009, 8:34:57 PM11/5/09
to android-...@googlegroups.com
On Thu, Nov 5, 2009 at 5:30 PM, Brian Cavagnolo <bcava...@gmail.com> wrote:
> Hello,
>
> I've got a project laid out like so:
>
> |-- Android.mk
> |-- AndroidManifest.xml
> `-- java
>    `-- com
>        `-- foo
>            |-- Foo.java
>            `-- FooTest.java
>
> Foo is a simple class.  It does not extend Activity or Application.
> FooTest.java extends TestCase class.  I successfully run the tests
> like so:
>
> $ adb shell am instrument -w com.foo/android.test.InstrumentationTestRunner
>
> Test results for InstrumentationTestRunner=
> Time: 0.001
>
> OK (0 tests)
>

CORRECTION: The output for the successful test is:

$ adb shell am instrument -w com.foo/android.test.InstrumentationTestRunner

com.foo.FooTest:.
Test results for InstrumentationTestRunner=.
Time: 0.025

OK (1 test)

Brian Cavagnolo

unread,
Nov 6, 2009, 2:35:33 PM11/6/09
to android-...@googlegroups.com
Figured out my problem. I needed to declare an application in the
AndroidManifest.xml file:

$ cat AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo">
<application android:name="FooApplication"/>
</manifest>
Reply all
Reply to author
Forward
0 new messages