Be Ready for TDD — Setup Android Studio for unit tests
I am not going to talk about how to write tests. Instead, this article covers how to setup environment for Unit Test.
Unit Test, DI ? Mock ?
Unit test sometimes is difficult since there are dependencies and you can not simply test a block of code. It gets worse as A is depending on B, B is depending on C, and so on. There are a couple of solutions such as dependency injection with Dagger1, 2. Though, if you have not worked on any of dependency injection before it is confusing. Even you are familiar with the concept, learning curve of Dagger is high.
Dagger is great but as I mentioned it is overwhelming for a beginner. So, today I am going to setup with an easier way.
Lovely build flavor
With Gradle, you can configure different build variants. Sometimes you want to have a different version for paid version to free version with keeping just 1 code base. Or you want to separate a build for production to staging. In our case for unit testing, I will just make “production” and “mock” flavors. “production” is for an app that I will deploy to Google Play Store. “mock” is for mocking some parts of class/functionality and make unit test testable.
First add this block inside of android block in app/build.gradle
productFlavors {
mock {
applicationId "com.emmasuzuki.azkaban.mocked"
}
production {
applicationId "com.emmasuzuki.azkaban"
}
}Then make your project structure looks like

You have androidTest and main by default so add “mock” and “production” folder. Under those folders, it should have at least “java” folder and under java you would have folder structure as “main” (com/emmasuzuki/azkaban/somepackage/SomeFile.java).
Anything that you would not need to interchange the implementation, you will put in main folder. Then under “production” and “mock”, you will put anything that requires to have a different implementation. Let’s use a simple example. Say, I want to have a mock class for REST call layer. How would I do that ? — I will create 2 classes which has same class name but placed in “production” and “mock” package.
production/java/com/emmasuzuki/azkaban/api/Dementors.java
public class Dementors { public void getHappiness(Callback callback) { restService.get("happiness", callback);}
}
mock/java/com/emmasuzuki/azkaban/api/Dementors.java
public class Dementors {
public void getHappiness(Callback callback) {Restponse<Happiness> json = getJsonFromLocalAsset("dementors_happiness.json");
callback.onSuccess(json);
}
}Now when you call “getHappiness” on production flavor, it is actually calling an restService’s get method whereas on mock flavor, it will return local json file loaded from mock/assets/json folder (free from dependency). The latter is the one we are using for unit tests.
Write Unit Test — Let’s start a journey
Before start writing it, you might notice on the image above. I have “test” folder which you would not have by default but I created it. Here I will put unit tests which does not require any Android framework, then I will run as JUnit runner which does not require an emulator or actual device.
Wait… don’t rush, not yet
If you just create “test” folder, you might see some errors, some files would not be recognized.
Gradle, the lovely gradle for help.
Add this block inside android block in app/build.gradle
sourceSets {
test {
java.srcDir 'src/test'
}
androidTest {
java.srcDir 'src/androidTest'
}
}This tells Android Studio that test folder has java sources and it is for unit tests. Likewise, androidTest folder is for Android Tests.
Okay, finally, it’s time to run unit tests on “mock” flavor
- Add test configuration
1. Right click on “test” folder and choose “Create Tests in ‘test’…”
2. Hit Okay - If build variants pane is not opened, tap build variants on left side bar
- Select mockDebug from build variant dropdown
- Hit Run
Build variants should look like this.

Now you realize there are 4 variants when I just set 2 flavors.
Build Variants = build flavors * build types
Android by default, there are debug and release build types, that result in 2 build flavors * 2 build types = 4 build variants.
How would I run unit tests from command line
Here is a gradle command for running all unit tests in mock flavor in debug mode. Run this at the top level of your project.
$./gradlew testMockDebugUnitTest
One last little tip.
You are like, mockRelease, it does not make sense. ‘release’, as it sounds, it is for release. You generally would not want to release with mocked implementation. Can I do something about that ?
Lovely Gradle again.
Add this block at the bottom of app/build.gradle
android.variantFilter { variant ->
if(variant.buildType.name.equals(‘release’) &&
variant.getFlavors().get(0).name.equals(‘mock’)) {
variant.setIgnore(true);
}
}This will remove mockRelease from build variants. One less variant to manage !
This is a start of TDD. In next article of this series, I will put more how-to of functional testing.