The Android framework includes an integrated testing framework that helps you test all aspects of your application and the SDK tools include tools for setting up and running test applications.
But at times you would require to write your own test cases to ensure that things work smoothly. There are different frameworks in android for the same. Few are listed below:
- JUnit
- Mockito
- Powermock
- Robolectric
- Espresso
- Hamcrest
Below is an example of JUnit
JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.
So let’s begin
- Set up the test environment:
dependencies {
// Required For JUnit4 testing
testCompile ‘junit:junit:4.12’
//For mocking using Mockito framework
testCompile ‘org.mockito:mockito-core:1.10.19’
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
}
In addition, to run Instrumented Unit test, we must add AndroidJUnitRunner
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
2. Create JUnit Test Class:
# Creating Local Test
Prior Android Studio 2.0.0: Open the Build Variants window by clicking the left-hand tab, then change the test artifact to Unit Tests.
The JUnit test class should be located at: app/src/test/java folder. Example; JUnit Test called CalculatorTest.java
public class CalculatorTest {
private Calculator mCalculator; @Before
public void setUp() throws Exception {
mCalculator = new Calculator();
} @Test
public void testSum() throws Exception {
//expected: 6, sum of 1 and 5
assertEquals(6d, mCalculator.sum(1d, 5d), 0);
}
}
# Creating Instrumented Test
Prior Android Studio 2.0.0: Open the Build Variants window by clicking the left-hand tab, then change the test artifact to Android Instrumentation Tests.
The JUnit Instrumented test class should be located at: app/src/androidTest/java folder. Example; JUnit Instrumented Test called LogHistoryAndroidUnitTest.java
@RunWith(AndroidJUnit4.class)
@SmallTest
public class LogHistoryAndroidUnitTest { public static final String TEST_STRING = "This is a string";
public static final long TEST_LONG = 12345678L;
private LogHistory mLogHistory; @Before
public void createLogHistory() {
mLogHistory = new LogHistory();
} @Test
public void logHistory_ParcelableWriteRead() {
// Set up the Parcelable object to send and receive.
mLogHistory.addEntry(TEST_STRING, TEST_LONG); // Write the data.
Parcel parcel = Parcel.obtain();
mLogHistory.writeToParcel(parcel, mLogHistory.describeContents()); // After you're done with writing, you need to reset the parcel for reading.
parcel.setDataPosition(0); // Read the data.
LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData(); // Verify that the received data is correct.
assertThat(createdFromParcelData.size(), is(1));
assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));
}
}
3. Run the Test.
We must chose the test artifact from Build Variants window to run either of those unit test. To run specific unit test class or method, right-click on it and Run ’test name’ it. To run all tests in the unit test directory, select the directory then right-click and press Run test.
Ta-da that’s it. Keep Coding!!