Unit Tests

Unit Tests

Overview

1) A unit test generally exercises the functionality of the smallest possible unit of code (which could be a method, class, or component)

2) You can use dependency providers like Robolectric or a mocking framework such as Mockito to isolate your unit from its dependencies.

3) Types of automated unit tests:

- Local tests: Unit tests that run on your local machine only.

- Instrumented tests: Unit tests that run on an Android device or emulator

Local Unit Test

1) Types of dependencies associated with your tests determine which tool you use:

  • If you have dependencies on the Android framework, particularly those that create complex interactions with the framework, it's better to include framework dependencies using Robolectric.
  • If your tests have minimal dependencies on the Android framework, or if the tests depend only on your own objects, it's fine to include mock dependencies using a mocking framework like Mockito.

2) Dependencies

dependencies {

 // Required -- JUnit 4 framework

 testImplementation 'junit:junit:4.12'

 // Optional -- Robolectric environment

 testImplementation 'androidx.test:core:1.0.0'

 // Optional -- Mockito framework

 testImplementation 'org.mockito:mockito-core:1.10.19'

 // Optional --Truth

 testImplementation "com.google.truth:truth:1.0.1"

}

3) comparing expected and actual results using junit.Assert methods or Hamcrest matchers or Truth

4) Unit test that uses Robolectric:

android {
    // ...
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

5) you can include Include mock dependencies & Android Builder classes like ApplicationInfo, PackageInfo , ... etc .

6) use a mocking framework to stub out external dependencies in your code By substituting Android dependencies with mock objects

7) With Mockito, you can configure mock objects to return some specific value when invoked.

8) To add a mock object to your local unit test using Mockito:

  • Include the Mockito library dependency
  • add the @RunWith(MockitoJUnitRunner.class)
  • add the @Mock annotation before the field declaration to create a mock object
  • To stub the behavior of the dependency, you can specify a condition and return value when the condition is met by using the when() and thenReturn() methods.

9) If your tests rely on your own dependencies, either provide your own fakes or mock the dependencies using a mock framework, such as Mockito

Instrumented Unit Test

1) dependencies

dependencies {

 androidTestImplementation 'androidx.test:runner:1.1.0'

 androidTestImplementation 'androidx.test:rules:1.1.0'

 // Optional -- Hamcrest library

 androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'

 // Optional -- UI testing with Espresso

 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

 // Optional -- UI testing with UI Automator

 androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

}

To use JUnit 4 test classes, make sure to specify AndroidJUnitRunner as the default test instrumentation

android {

 defaultConfig {

 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

 }

}

2) @RunWith(AndroidJUnit4:: class) is required only if you use a mix of JUnit3 and JUnit4.

3) you can group a collection of test classes in a test suite

4) To create a test suite for your unit tests

  • @RunWith(Suite::class)
  • @Suite.SuiteClasses(ClassXInstrumentationTest::class,

ClassXTest::class)

5) Run your tests with Firebase Test Lab

https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests#run-ctl

To view or add a comment, sign in

More articles by Noha Samir

Explore content categories