public abstract class

ServiceTestCase

extends AndroidTestCase
java.lang.Object
   ↳ junit.framework.Assert
     ↳ junit.framework.TestCase
       ↳ android.test.AndroidTestCase
         ↳ android.test.ServiceTestCase<T extends android.app.Service>

Class Overview

This test case provides a framework in which you can test Service classes in a controlled environment. It provides basic support for the lifecycle of a Service, and hooks by which you can inject various dependencies and control the environment in which your Service is tested.

Lifecycle Support. Every Service is designed to be accessed within a specific sequence of calls. . In order to support the lifecycle of a Service, this test case will make the following calls at the following times.

  • The test case will not call onCreate() until your test calls startService(Intent) or bindService(Intent). This gives you a chance to set up or adjust any additional framework or test logic before onCreate().
  • When your test calls startService(Intent) or bindService(Intent) the test case will call onCreate(), and then call the corresponding entry point in your service. It will record any parameters or other support values necessary to support the lifecycle.
  • After your test completes, the test case tearDown() function is automatically called, and it will stop and destroy your service with the appropriate calls (depending on how your test invoked the service.)

Dependency Injection. Every service has two inherent dependencies, the Context in which it runs, and the Application with which it is associated. This framework allows you to inject modified, mock, or isolated replacements for these dependencies, and thus perform a true unit test.

If simply run your tests as-is, your Service will be injected with a fully-functional Context, and a generic MockApplication object. You can create and inject alternatives to either of these by calling setContext() or setApplication(). You must do this before calling startService() or bindService(). The test framework provides a number of alternatives for Context, including {link android.test.mock.MockContext MockContext}, RenamingDelegatingContext, and ContextWrapper.

Summary

[Expand]
Inherited Fields
From class android.test.AndroidTestCase
Public Constructors
ServiceTestCase(Class<T> serviceClass)
Public Methods
Application getApplication()
Return the Application object being used by the Service under test.
T getService()
Context getSystemContext()
Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Service under test.
void setApplication(Application application)
Set the application for use during the test.
void testServiceTestCaseSetUpProperly()
Protected Methods
IBinder bindService(Intent intent)
Start the service under test, in the same way as if it was started by Context.bindService(), providing the arguments it supplied.
void setUp()
This will do the work to instantiate the Service under test.
void setupService()
Create the service under test and attach all injected dependencies (Context, Application) to it.
void shutdownService()
This will make the necessary calls to stop (or unbind) the Service under test, and call onDestroy().
void startService(Intent intent)
Start the service under test, in the same way as if it was started by Context.startService(), providing the arguments it supplied.
void tearDown()
Shuts down the Service under test.
[Expand]
Inherited Methods
From class android.test.AndroidTestCase
From class junit.framework.TestCase
From class junit.framework.Assert
From class java.lang.Object
From interface junit.framework.Test

Public Constructors

public ServiceTestCase (Class<T> serviceClass)

Public Methods

public Application getApplication ()

Return the Application object being used by the Service under test.

Returns
  • Returns the application object.

public T getService ()

Returns
  • Returns the actual service under test.

public Context getSystemContext ()

Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Service under test.

Returns
  • Returns a reference to a normal Context.

public void setApplication (Application application)

Set the application for use during the test. If your test does not call this function, a new MockApplication object will be generated.

Parameters
application The Application object that will be injected into the Service under test.

public void testServiceTestCaseSetUpProperly ()

Throws
Exception

Protected Methods

protected IBinder bindService (Intent intent)

Start the service under test, in the same way as if it was started by Context.bindService(), providing the arguments it supplied. Return the communication channel to the service. May return null if clients can not bind to the service. The returned IBinder is usually for a complex interface that has been described using aidl. Note: In order to test with this interface, your service must implement a getService() method, as shown in samples.ApiDemos.app.LocalService.

Parameters
intent The Intent as if supplied to bindService(Intent, ServiceConnection, int).
Returns
  • Return an IBinder for making further calls into the Service.

protected void setUp ()

This will do the work to instantiate the Service under test. After this, your test code must also start and stop the service.

Throws
Exception

protected void setupService ()

Create the service under test and attach all injected dependencies (Context, Application) to it. This will be called automatically by startService(Intent) or by bindService(Intent). If you wish to call setContext() or setApplication(), you must do so before calling this function.

protected void shutdownService ()

This will make the necessary calls to stop (or unbind) the Service under test, and call onDestroy(). Ordinarily this will be called automatically (by tearDown(), but you can call it directly from your test in order to check for proper shutdown behaviors.

protected void startService (Intent intent)

Start the service under test, in the same way as if it was started by Context.startService(), providing the arguments it supplied. If you use this method to start the service, it will automatically be stopped by tearDown().

Parameters
intent The Intent as if supplied to startService(Intent).

protected void tearDown ()

Shuts down the Service under test. Also makes sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.

Throws
Exception