1

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);
    }

}
Community
  • 1
  • 1
MDM82
  • 71
  • 3
  • After debugging I found that getContext() is null, so I'm guessing that why my intent is not launching? I tried creating a mock context (see below) – MDM82 May 24 '15 at 20:09
  • final String prefix = "test"; MockContentResolver resolver = new MockContentResolver(); RenamingDelegatingContext target = new RenamingDelegatingContext(new MockContext(), getContext(),prefix); IsolatedContext newContext = new IsolatedContext(resolver, target); – MDM82 May 24 '15 at 20:09
  • Even though this context isn't null, the test still fails. – MDM82 May 24 '15 at 20:10

1 Answers1

0

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

ATSL link

It seems they forgot to update the actual documentation.

Herrbert74
  • 2,578
  • 31
  • 51