【问题标题】:ServiceTestCase is not launching my serviceServiceTestCase 没有启动我的服务
【发布时间】:2012-01-20 15:51:56
【问题描述】:

我正在尝试为我的 android 应用程序编写一些 JUnit 测试。该应用程序是一项服务。

我已经尝试了几种方法来让 ServiceTestCase 启动服务,但都失败了。

但是,当我调试 ServiceTestCase 时,它​​将启动该服务。我相信这是因为 ServiceTestCase 正在调用 setup 并且在它杀死它之前没有给服务启动足够的时间......

我不完全确定,这是我第一次为 android 使用 junit 测试。对此非常新。有什么建议可以解决这个问题吗?

我正在考虑创建一些计时器循环或其他东西,但这似乎真的很脏。想要一个更清洁的方法。


Android Manifest for the service.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dataservice.server"
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name="dataservice.server.DataServer"  android:process=":remote" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="dataservice.DataService.BIND.1" />
            </intent-filter>
            <intent-filter>
                <action android:name= ".DataServer" />
            </intent-filter>
        </service>
        <receiver android:name="dataservice.server.Receiver">
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT"></action>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

The ServiceTestCase i am attempting.
public class Publish extends ServiceTestCase<DataServer> {

    private final static String TAG = "Publish Unit Test";
    private Context mSystemContext;

    public Publish() {
        super(DataServer.class);
    }

    public Publish(Class<DataServer> serviceClass) {
        super(serviceClass);
    }

    @Override
    protected void setUp() throws Exception {
        // TODO Auto-generated method stub
        super.setUp();
                // this is where i am attempting to start the service.
                // i have attempted other methods, but none of those worked either.
                startService(new Intent(this.getContext(), DataServer.class));
    }

    public void testPublish() {
      }
}

【问题讨论】:

    标签: android unit-testing testing junit servicetestcase


    【解决方案1】:

    我错误地引用了其中一个依赖项目。一旦解决了这个问题,它就解决了这个问题。因此,如果您遇到同样的问题,请检查并确保您正确引用了您的项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 2017-03-21
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多