lucasweb lucasweb - 5 months ago 9
Android Question

Can not get Android ServiceTestCase to run

I am unable to get any test cases that extend ServiceTestCase to run. There are no errors they are just not executed.

Other test cases that extend AndroidTestCase do run.

The projects are set up as follows:

I have a Android Library that contains a service. It's manifest file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.something.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
<service android:name=".ExampleService"
android:exported="false"
android:process=":example_service">
</service>
</application>
</manifest>


The Android Library Project contains a test project in the folder test (created using the Android tools)

This contains a AndroidManfiest.xml as as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.something.android.tests"
android:versionCode="1"
android:versionName="1.0">
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.something.android.tests"
android:label="Tests for com.something.android"/>
</manifest>


I also have a build.properties in the test project which contains:

tested.project.dir=..
android.library.reference.1=..

I execute the tests by running ant clean run-tests.

What do I need to do to get the ServiceTestCase test to run?

Thanks in advance.

Answer

Make sure that you provide a default constructor for the ServiceTestCase. The one Eclipse generates for you may not be appropriate. For example, in my case Eclipse generated for me:

public class MyServiceTestCase extends ServiceTestCase<MyService> {

  public MyServiceTestCase(Class<MyService> serviceClass) {
    super(serviceClass);
  }
  ...
}

Android JUnit was not able to instantiate MyServiceTestCase, but it did not complain, and I only could see that no test cases where run.

Thefore I replaced the constructor with the following, and it worked nicely:

public class MyServiceTestCase extends ServiceTestCase<MyService> {

  public MyServiceTestCase() {
    super(MyService.class);
  }
  ...
}

Hope it works for you.

Comments