Android ServiceTestCase keeps failing?

335 Views Asked by At

I am new to Android Studio and I am trying to test a GPS service. I actually used an example I found in another posting by @dtmilano I'm not getting any errors, but the tests fail. Another posting mentioned adding thread.sleep() after starting the service in the test, but it still fails. Can someone please tell me what I am missing?

This is the example I used: ServiceTestCase<T>.getService?

import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.test.MoreAsserts;
import android.test.ServiceTestCase;


public class GPSServiceTest extends ServiceTestCase<GPSService> {


    public GPSServiceTest() {
        super(GPSService.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }


    public void testOnStartCommand() throws Exception {
        Intent startIntent = new Intent();
        startIntent.setClass(getContext(), GPSService.class);
        startService(startIntent);
        Thread.sleep(10 * 1000);
        assertNotNull(getService());
    }

    public void testBindable() {
        Intent startIntent = new Intent();
        startIntent.setClass(getContext(), GPSService.class);
        IBinder service = bindService(startIntent);
        assertNotNull(service);
    }

}
1

There are 1 best solutions below

0
Herrbert74 On

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

ATSL link

It seems they forgot to update the actual documentation.