java.lang.Object | |
↳ | android.support.test.rule.ServiceTestRule |
A JUnit rule that provides a simplified mechanism to start and shutdown your service before
and after the duration of your test. It also guarantees that the service is successfully
connected when starting (or binding to) a service. The service can be started
(or bound) using one of the helper methods. It will automatically be stopped (or unbound) after
the test completes and any methods annotated with
After
are
finished.
Note: This rule doesn't support IntentService
because it's automatically
destroyed when onHandleIntent(android.content.Intent)
finishes
all outstanding commands. So there is no guarantee to establish a successful connection
in a timely manner.
Usage:
@Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); @Test public void testWithStartedService() { mServiceRule.startService( new Intent(InstrumentationRegistry.getTargetContext(), MyService.class)); //do something } @Test public void testWithBoundService() { IBinder binder = mServiceRule.bindService( new Intent(InstrumentationRegistry.getTargetContext(), MyService.class)); MyService service = ((MyService.LocalBinder) binder).getService(); assertTrue("True wasn't returned", service.doSomethingToReturnTrue()); }
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a
ServiceTestRule with a default timeout of 5 seconds
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Starts the service under test, in the same way as if it were started by
Context.bindService(Intent, ServiceConnection, flags) with an
Intent that identifies a service.
| |||||||||||
Works just like
bindService(android.content.Intent, android.content.ServiceConnection, int) except
uses an internal ServiceConnection to guarantee successful bound.
| |||||||||||
Starts the service under test and blocks until the service is connected, in the same way as
if it were started by
Context.startService(Intent) with an Intent that identifies a
service.
| |||||||||||
Factory method to create a
ServiceTestRule with a custom timeout
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Override this method to do your own service specific clean up after the service is shutdown.
| |||||||||||
Override this method to do your own service specific initialization before starting or
binding to the service.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
org.junit.rules.TestRule
|
Starts the service under test, in the same way as if it were started by
Context.bindService(Intent, ServiceConnection, flags)
with an
Intent
that identifies a service. However, it waits for
onServiceConnected(android.content.ComponentName, android.os.IBinder)
to be called before returning.
intent | Identifies the service to connect to. The Intent may
specify either an explicit component name, or a logical
description (action, category, etc) to match an
IntentFilter published by a service. |
---|---|
connection | Receives information as the service is started and stopped. This must be a valid ServiceConnection object; it must not be null. |
flags | Operation options for the binding. May be 0,
BIND_AUTO_CREATE ,
BIND_DEBUG_UNBIND ,
BIND_NOT_FOREGROUND ,
BIND_ABOVE_CLIENT ,
BIND_ALLOW_OOM_MANAGEMENT , or
BIND_WAIVE_PRIORITY . |
SecurityException | if the called doesn't have permission to bind to the given service. |
---|---|
TimeoutException | if timed out waiting for a successful connection with the service. |
Works just like
bindService(android.content.Intent, android.content.ServiceConnection, int)
except
uses an internal ServiceConnection
to guarantee successful bound.
The operation option flag defaults to BIND_AUTO_CREATE
TimeoutException |
---|
Starts the service under test and blocks until the service is connected, in the same way as
if it were started by Context.startService(Intent)
with an Intent
that identifies a
service. If you use this method to start the service, it is automatically stopped at the
end of the test run. However, it also binds to the service and waits for
onServiceConnected(android.content.ComponentName, android.os.IBinder)
to be called to ensure successful connection.
intent | An Intent that identifies a service, of the same form as the Intent passed to
Context.startService
(Intent) . |
---|
SecurityException | if you do not have permission to bind to the given service. |
---|---|
TimeoutException | if timed out waiting for a successful connection with the service. |
Factory method to create a ServiceTestRule
with a custom timeout
timeout | the amount of time to wait for a service to connect. |
---|---|
timeUnit | the time unit representing how the timeout parameter should be interpreted |
ServiceTestRule
with the desired timeout
Override this method to do your own service specific clean up after the service is shutdown.
The method is called after each test method is executed including any method annotated with
After
and after necessary calls to stop (or unbind) the service under test were called.
Override this method to do your own service specific initialization before starting or
binding to the service. The method is called before each test method is executed including
any method annotated with
Before
.
Do not start or bind to a service from here!