This rule 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
IntentService#onHandleIntent(android.content.Intent)
finishes all outstanding commands. So there is no guarantee to establish a successful connection in a timely manner.
This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended.
git clone
../build.gradle
file.src/main/java
src/androidTest/java
The application will be started on the device/emulator and a series of actions will be performed automatically.
If you are using Android Studio, the Run window will show the test results.
compileSdkVersion 26
applicationId "com.example.android.testing.integrationtesting.ServiceTestRuleSample"
minSdkVersion 9
targetSdkVersion 26
versionCode 1
versionName "1.0"
package com.example.android.testing.ServiceTestRuleSample
{@link Service} that generates random numbers.
A seed for the random number generator can be set via the {@link Intent} passed to {@link #onBind(Intent)}.
Returns a random integer in [0, 100).