Can not get Android ServiceTestCase to run

霸气de小男生 提交于 2019-12-07 06:35:09

问题


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.


回答1:


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.




回答2:


The accepted answer doesn't work for me any more.

TestCases like ActivityInstrumentationTestCase2 or ServiceTestCase are deprecated in favor of ActivityTestRule or ServiceTestRule.

ATSL link

It seems they forgot to update the actual documentation.



来源:https://stackoverflow.com/questions/7855479/can-not-get-android-servicetestcase-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!