In my last blog post, you have learnt how to introduce Robolectric. Unfortunately, when you start using Android flavors, the whole concept starts to collapse.

This second part of my Robolectric series offers you a brief tutorial, deduced from Jason Atwood’s perceptions about Robolectric with flavors, how to master flavors with Robolectric.

Flavors

Widely known, product flavors can be added via Gradle.

By the way: Even when Android Studio begs you to cast your product flavors application ids: Don’t do it.

Issue

When you try to run an Robolectric tests, they will fail because your application id cannot be resolved.

Fortunately, due to the current Robolectric 3.0 release, you can define a packageName identifier to fix your broken tests.

Test Directories

From a directory structure point of view, flavors are realized by adding additional directories to your source folder. I specified the two flavors “free” and “pro”, so I created their related source folders beside the main directory.

  • src/free
  • src/pro

Depending on your ‘Build Variants’, the proper flavor directory is highlighted in blue to emphasize which source folders are being checked while programming and considered for device or emulator deployment.

FlavorsTestDirectories

Flavor project src structure

Regarding project structure, beside the test source folders ‘androidTest’ and ‘test‘, you should create test directories matching your product flavor names:

  • src/testFree
  • src/testPro

Based on your build variants, you can switch between your flavor based test source folders for execution.

FlavorTestDirectories

Flavor project test structure

Test Class Naming

Generally known, you cannot add two classes with the same name and package identifier in two different flavors, except asset or resource files. Therefore, your test classes’ names must differ.

FlavorTestClassNaming

Flavor project class naming

If you want to auto-create a (flavored) test case, open your desired class and use the Ctrl + Shift + T shortcut.

CreateTest

Create Flavor Test

After clicking the ‘OK’ button, choose your preferred test (flavor) folder.

ChooseFlavorTestDirectory

Choose Flavor Test Directory

Remark: Depending on your chosen build variant, only the specified flavor test folder will be listed.

Test Scenario

In my case, I wanted to test if my FloatingActionButton’s color is properly customized when applying my pro flavor sources. For your interest: I used the BasicActivity example from Android Studio for this demo.

FlavorDeepPinkButton

Basic Activity Android Studio – Deep Pink

FlavorRedButton

Basic Activity Android Studio – Red

Both flavors have in common that there activity title matches in both variants. So I implemented a test, located in src/test/MainActivityTest, to assert all common functionality of both flavors whereas separate test classes are located in src/testFree and src/testPro to assert the FloatingActionButton color behavior.

Test Class

In the end, the flavor test case can be look as follows:

The crucial line of code is the declaration of a packageName to make Robolectric compatible with flavors.

Summary

You are now capable of adapting your Robolectric test classes for flavors and how your project structure has to be correctly designed to switch between flavors for different test executions.

— Stefan

Leave a Comment

2 comments

  1. D

    Good example to test the differences between your activities. But as I understand at some point you will start duplicating your tests…

  2. S

    Hi Diogo,

    it’s one of Android flavor’s drawbacks that, up to a certain point, code become redundant. Unfortunately, this flavor behavior reflects on our tests as well.

    In the end, it depends on the redundancy (and thereby of the design) of the actual app code.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close