无法让 Android ServiceTestCase 运行

2023-12-29

我无法获得任何扩展 ServiceTestCase 来运行的测试用例。没有错误,只是没有执行。

扩展 Android 测试用例运行的其他测试用例。

项目设置如下:

我有一个包含服务的 Android 库。其清单文件如下:

<?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>

Android 库项目在文件夹 test 中包含一个测试项目(使用 Android 工具创建)

其中包含一个AndroidManfiest.xml如下

<?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>

我的测试项目中还有一个 build.properties,其中包含:

测试的.project.dir=.. android.library.reference.1=..

我通过运行 ant clean run-tests 来执行测试。

我需要做什么才能运行 ServiceTestCase 测试?

提前致谢。


确保为 ServiceTestCase 提供默认构造函数。 Eclipse 为您生成的那个可能不合适。例如,在我的例子中,Eclipse 为我生成了:

public class MyServiceTestCase extends ServiceTestCase<MyService> {

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

Android JUnit 无法实例化 MyServiceTestCase,但它没有抱怨,我只能看到没有测试用例运行。

因此,我用以下内容替换了构造函数,并且效果很好:

public class MyServiceTestCase extends ServiceTestCase<MyService> {

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

希望对你有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法让 Android ServiceTestCase 运行 的相关文章

随机推荐