Book Image

Learning Android Application Testing

Book Image

Learning Android Application Testing

Overview of this book

Table of Contents (16 chapters)
Learning Android Application Testing
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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...