If you are new to unit testing on Android, try this sample first.
This project uses the Gradle build system and the Android gradle plugin support for unit testing. You can either benefit from IDEs integration such as Android studio or run the tests on the command line.
Unit tests run on a local JVM on your development machine. The Android Gradle plugin will compile your app's source code and execute it using gradle test task. Tests are executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.
For more information see http://tools.android.com/tech-docs/unit-testing-support
git clone
../build.gradle
file.src/main/java
src/test/java
org.junit.runners.JUnit4
The unit test will be ran automatically.
After downloading the projects code using git clone
you'll be able to run the
unit tests using the command line:
./gradlew test
If all the unit tests have been successful you will get a BUILD SUCCESSFUL
message.
A report in HTML format is generated in app/build/reports/tests
compileSdkVersion 26
applicationId "com.example.android.testing.unittesting.BasicSample"
minSdkVersion 9
versionCode 1
versionName "1.0"
targetSdkVersion 26
testCompile 'junit:junit:' + rootProject.junitVersion;
testCompile 'org.mockito:mockito-core:' + rootProject.mockitoVersion;
package com.example.android.testing.unittesting.BasicSample
An {@link Activity} that represents an input form page where the user can provide his name, date of birth and email address. The personal information can be saved to {@link SharedPreferences} by clicking a button.
Initialize all fields from the personal info saved in the SharedPreferences.
Called when the "Save" button is clicked.
Called when the "Revert" button is clicked.