An Introduction to Dagger 2 (Android DI) - Part 4
This article focuses on introducing a powerful test coverage technique for ensuring that the app unit tests are strong enough.
Join the DZone community and get the full member experience.
Join For FreeParts 1, 2, and 3 showed an introduction to Dagger, MVP pattern, Espresso, and more. This article focuses on introducing a powerful test coverage technique for ensuring that the app unit tests are strong enough.
Introduction to Mutation Testing
One of the golden standard test coverage techniques is mutation testing. The main idea of mutation testing is to perform byte code modifications (mutations) to your original app source code and then run your app unit tests to check if they are strong enough to fail as a result of these mutations.
The byte code changes in Mutation Testing are named as faults (or mutations). After applying the source code changes, if your tests fail then the mutation is killed, and if your tests pass then the mutation is lived.
So in summary, if your tests fail by source code mutations then this means that your tests are really strong, if your tests pass then this means you need to modify your tests to make them strong enough (The quality of your tests can be gauged from the percentage of mutations killed).
Why We Need Mutation Testing
Mutation tests have an advantage over Traditional test coverage (i.e line, statement, branch etc) because it does not only measure which code is executed by your tests but also it checks that your tests are actually able to detect faults in the executed code. This gives a strong measurement to your developed unit tests.
PIT
PIT is one of the mutation testing systems for Java. It has several advantages:
- Fast to execute.
- Integrated with Java build tools (Ant, Maven, and Gradle).
- Active.
More information about PIT can be found at:
http://pitest.org/
Android Integration
Unfortunately, the official PIT Gradle plugin does not work fine with Android Gradle projects:
https://github.com/szpak/gradle-pitest-plugin
However, thanks to one of the community member, this support is now possible using this fork:
https://github.com/koral–/gradle-pitest-plugin
Application Configuration
In order to configure Android PIT plugin with our Android Dagger app:
Add PIT plugin to your top-level
build.gradle
file:
classpath 'pl.droidsonroids.gradle:gradle-pitest-plugin:0.0.3'
Configure PIT plugin in your app
build.gradle
file:
pitest {
targetClasses = ['com.test.xyz.daggersample1.ui.presenter.*'] /* specify target classes to be mutated */
excludedClasses = ['**Factory*'] /* Exclude Dagger generated classes */
pitestVersion = "1.1.0"
threads = 4 /* specify number of threads */
outputFormats = ['XML', 'HTML'] /* specify output format */
}
Checking Results
After configuration, finally, we can execute PIT tests by running:
./gradlew pitest
You can pick the PIT tests report under build/reports/pitest
.
The following figure shows the mutation testing report in our Dagger sample:
The following figure shows survived and killed mutants:
FYI, in the mutation report, Light green shows line coverage, dark green shows mutation coverage, Light pink show lack of line coverage, and dark pink shows a lack of mutation coverage.
Source code
App Source code is available in:
https://github.com/hazems/Dagger-Sample/tree/pit1/app/src/test/java/com/test/xyz/daggersample1/ui/presenter
Top-level Gradle file PIT configuration:
https://github.com/hazems/Dagger-Sample/blob/pit1/build.gradle
Module-level Gradle file PIT configuration:
https://github.com/hazems/Dagger-Sample/blob/pit1/app/build.gradle
In our next article, let’s see how to kill these survived mutants which make our tests not strong enough!
Previous articles in this series:
https://dzone.com/articles/an-introduction-to-dagger-2-android-di-part-1-3
https://dzone.com/articles/an-introduction-to-dagger-2-android-di-part-2-2
https://dzone.com/articles/dagger-2-mvp-and-unit-testing-android-di-part-3-1
Published at DZone with permission of Hazem Saleh, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments