Added in API level 1
Deprecated in API level 24

ServiceTestCase


abstract class ServiceTestCase<T : Service!> : AndroidTestCase
kotlin.Any
   ↳ android.test.AndroidTestCase
   ↳ android.test.ServiceTestCase

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 with which you can inject various dependencies and control the environment in which your Service is tested.

Lifecycle Support. A Service is accessed with a specific sequence of calls, as described in the Services document. In order to support the lifecycle of a Service, ServiceTestCase enforces this protocol:

  • The setUp() method is called before each test method. The base implementation gets the system context. If you override setUp(), you must call super.setUp() as the first statement in your override.
  • The test case waits to call android.app.Service#onCreate() until one of your test methods calls startService or bindService. This gives you an opportunity to set up or adjust any additional framework or test logic before you test the running service.
  • When one of your test methods calls ServiceTestCase.startService() or ServiceTestCase.bindService(), the test case calls Service.onCreate() and then calls either Service.startService(Intent) or android.app.Service#bindService(Intent, ServiceConnection, int), as appropriate. It also stores values needed to track and support the lifecycle.
  • After each test method finishes, the test case calls the tearDown method. This method stops and destroys the service with the appropriate calls, depending on how the service was started. If you override tearDown(), your must call the super.tearDown() as the last statement in your override.

Dependency Injection. A service has two inherent dependencies, its Context and its associated Application. The ServiceTestCase framework allows you to inject modified, mock, or isolated replacements for these dependencies, and thus perform unit tests with controlled dependencies in an isolated environment.

By default, the test case is injected with a full system context and a generic MockApplication object. You can inject alternatives to either of these by invoking setContext() or setApplication(). You must do this before calling startService() or bindService(). The test framework provides a number of alternatives for Context, including MockContext, RenamingDelegatingContext, ContextWrapper, and android.test.IsolatedContext.

Summary

Public constructors
ServiceTestCase(serviceClass: Class<T>!)

Constructor

Public methods
open Application!

Returns the Application object in use by the service under test.

open T

open Context!

Returns the real system context that is saved by setUp().

open Unit
setApplication(application: Application!)

Sets the application that is used during the test.

open Unit

Tests that setupService() runs correctly and issues an junit.

Protected methods
open IBinder!
bindService(intent: Intent!)

Starts the service under test, in the same way as if it were started by android.

open Unit

Gets the current system context and stores it.

open Unit

Creates the service under test and attaches all injected dependencies (Context, Application) to it.

open Unit

Makes the necessary calls to stop (or unbind) the service under test, and calls onDestroy().

open Unit
startService(intent: Intent!)

Starts the service under test, in the same way as if it were started by Context.startService(Intent) with an android.content.Intent that identifies a service.

open Unit

Shuts down the service under test.

Inherited functions
Inherited properties

Public constructors

ServiceTestCase

Added in API level 1
ServiceTestCase(serviceClass: Class<T>!)

Constructor

Parameters
serviceClass Class<T>!: The type of the service under test.

Public methods

getApplication

Added in API level 1
open fun getApplication(): Application!

Deprecated: Deprecated in Java.

Returns the Application object in use by the service under test.

Return
Application! The application object.

See Also

getService

Added in API level 1
open fun getService(): T

Deprecated: Deprecated in Java.

Return
T An instance of the service under test. This instance is created automatically when a test calls startService or bindService.

getSystemContext

Added in API level 1
open fun getSystemContext(): Context!

Deprecated: Deprecated in Java.

Returns the real system context that is saved by setUp(). Use it to create mock or other types of context objects for the service under test.

Return
Context! A normal system context.

setApplication

Added in API level 1
open fun setApplication(application: Application!): Unit

Deprecated: Deprecated in Java.

Sets the application that is used during the test. If you do not call this method, a new MockApplication object is used.

Parameters
application Application!: The Application object that is used by the service under test.

testServiceTestCaseSetUpProperly

Added in API level 1
open fun testServiceTestCaseSetUpProperly(): Unit

Deprecated: Deprecated in Java.

Tests that setupService() runs correctly and issues an junit.framework.Assert#assertNotNull(String, Object) if it does. You can override this test method if you wish.

Exceptions
java.lang.Exception

Protected methods

bindService

Added in API level 1
protected open fun bindService(intent: Intent!): IBinder!

Deprecated: Deprecated in Java.

Starts the service under test, in the same way as if it were started by android.content.Context#bindService(Intent, ServiceConnection, int) with an android.content.Intent that identifies a service.

Notice that the parameters are different. You do not provide a android.content.ServiceConnection object or the flags parameter. Instead, you only provide the Intent. The method returns an object whose type is a subclass of android.os.IBinder, or null if the method fails. An IBinder object refers to a communication channel between the application and the service. The flag is assumed to be android.content.Context#BIND_AUTO_CREATE.

See Designing a Remote Interface Using AIDL for more information about the communication channel object returned by this method.

Note: To be able to use bindService in a test, the service must implement getService() method. An example of this is in the ApiDemos sample application, in the LocalService demo.
Parameters
intent Intent!: An Intent object of the form expected by android.content.Context#bindService.
Return
IBinder! An object whose type is a subclass of IBinder, for making further calls into the service.

setUp

Added in API level 1
protected open fun setUp(): Unit

Deprecated: Deprecated in Java.

Gets the current system context and stores it. Extend this method to do your own test initialization. If you do so, you must call super.setUp() as the first statement in your override. The method is called before each test method is executed.

setupService

Added in API level 1
protected open fun setupService(): Unit

Deprecated: Deprecated in Java.

Creates the service under test and attaches all injected dependencies (Context, Application) to it. This is called automatically by startService or by bindService. If you need to call setContext() or setApplication(), do so before calling this method.

shutdownService

Added in API level 1
protected open fun shutdownService(): Unit

Deprecated: Deprecated in Java.

Makes the necessary calls to stop (or unbind) the service under test, and calls onDestroy(). Ordinarily this is called automatically (by tearDown, but you can call it directly from your test in order to check for proper shutdown behavior.

startService

Added in API level 1
protected open fun startService(intent: Intent!): Unit

Deprecated: Deprecated in Java.

Starts the service under test, in the same way as if it were started by Context.startService(Intent) with an android.content.Intent that identifies a service. If you use this method to start the service, it is automatically stopped by tearDown.

Parameters
intent Intent!: An Intent that identifies a service, of the same form as the Intent passed to Context.startService(Intent).

tearDown

Added in API level 1
protected open fun tearDown(): Unit

Deprecated: Deprecated in Java.

Shuts down the service under test. Ensures all resources are cleaned up and garbage collected before moving on to the next test. This method is called after each test method.

Subclasses that override this method must call super.tearDown() as their last statement.

Exceptions
java.lang.Exception