Testing local and remote services
When you want to test an android.app.Service
, the idea is to extend the ServiceTestCase<Service>
class to test in a controlled environment:
public class DummyServiceTest extends ServiceTestCase<DummyService> { public DummyServiceTest() { super(DummyService.class); } public void testBasicStartup() { Intent startIntent = new Intent(); startIntent.setClass(getContext(), DummyService.class); startService(startIntent); } public void testBindable() { Intent startIntent = new Intent(); startIntent.setClass(getContext(), DummyService.class); bindService(startIntent); } }
The constructor, as in other similar cases, invokes the parent constructor that passes the Android service class as a parameter.
This is followed by testBasicStartup()
. We start the service using an Intent that we create here, setting its class to the class of the service under test. We also use the instrumented...