Search code examples
androidunit-testingtestingjunitservicetestcase

ServiceTestCase is not launching my service


I am attempting to write some JUnit tests for my android application. The application is a service.

I have tried several things to get the ServiceTestCase to launch the service but it fails to.

HOWEVER, when i debug the ServiceTestCase it WILL launch the service. I belive this is because the ServiceTestCase is calling setup and not giving enough time for the service to launch before it kills it...

I am not completely sure, this is the first time i have ever used junit testing for android. VERY new to this. Any suggestions on what i can do to fix this problem?

I was thinking of creating some timer loop or something, but that seems REALLY dirty. Would like a much cleaner aproach.


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() {
      }
}

Solution

  • I was referencing one of the dependant projects incorrectly. Once this was fixed, it resolved the issue. So, check and make sure you are referencing your projects correctly if you are having the same issue.