user-icon Stefan Nägele
22. April 2016
timer-icon 4 min

Testing with Robolectric - Configuration

Set Of Problems

Writing unit tests with the default Android Testing Support library is a time-consuming job. Although it is already possible to use JUnit 4 instead of the more cumbersome JUnit 3 syntax, it is still hard to get feedback fast on your recently written code, especially if you want to apply a Test Driven Development approach (TDD).

Android Studio does support alternate JUnit and Android testing executions. Code which is actually written in pure Java without any dependencies to the Android SDK, e.g. the business or model layer, can be tested immediately with the standard JUnit 4 framework. Nevertheless, code using the Android SDK requires the Android Testing Support library. Running actual Android tests take several seconds to get a final feedback because the productive code and its test cases have to be deployed on the Android emulator or an actual device. In the end, if you want to develop in a TDD manner, the testing lifecycle of

  • writing tests
  • running tests
  • tests are red
  • implementing your methods
  • re-running the test
  • tests are getting green

takes lots of effort due to the long execution time. You are blocked by a long running test execution instead of doing your actual job.

Don’t get me wrong, the Android Testing Support Library still fits the needs for long running integration tests but the characteristic and demands of real unit tests are different compared to what the Android Testing Supporting library is capable of offering.

Robolectric

That’s why I make use of Robolectric. Robolectric is capable of testing pure Java and Android code within the JVM as well, independently from a running Android environment.

Although Robolectric is already a wide-spread library, its configuration and usage is not documented in-depth. Most Robolectric tutorials require a bunch of tacit knowledge about Android Studio or IntelliJ. This makes it more difficult to motivate developers – especially beginners – to add unit tests. Therefore, this blog post aims to provide a straightforward step by step tutorial on how to establish Robolectric in your Android project. Missing Android unit tests should no longer be an excuse due to the default Google provided testing frameworks or documentation.

Gradle

First of all, we need the Robolectric dependency. Due to Gradle as build management tool, it is straightforward. Open the build.gradle script of your actual app module which is responsible for your dependencies.

RobolectricGradle

Target build.gradle script

Afterwards, the build.gradle dependency section has to be extended by the Robolectric library.


When you add Robolectric as test scope dependency, be aware that there is a fundamental difference between the keywords testCompile and testAndroidCompile. Using testAndroidCompile commands Android Studio and the Gradle build to run Robolectric tests on an emulator or device. In the end, Robolectric tests would fail.

Project Structure

Meanwhile, Android Studio 2.0 auto-creates a proper test folder structure when creating a new Android project.

  • app/src/androidTest/java
  • app/src/main/java
  • app/src/test/java

Previous created apps depending on your used Android Studio version, do neither contain a src/androidTest/java nor a src/test/java folder. You have to add this folders manually.

TestFolderStructure

Test folder project structure

The default test folder src/test/java should already be common to every Java programmer. Therefore, because we want to write tests which can be executed without a device or emulator, we use this folder to administrate our Robolectric unit tests. The androidTest should contain classes which require an emulator or device.

Build Variants

Now, if you want to run pure JUnit / Robolectric tests from your IDE, switch your build variants from Android Instrumentation Tests to Unit Tests. src/test/java should now be enabled as your green highlighted test source folder.

BuildVariantsUnitTests

Build Variants ‘Unit Tests’

HighlightedTestFolder

Highlighted src/test/java folder

By the way: In Android Studio 2.0, this step becomes obsolete. From now on, test folders are detected automatically, so that a ‘Test Artifact’ need not / cannot be chosen any longer.

Creating A Test

After a successful Gradle configuration and project structure preparation, auto-create your test template by opening an arbitrary class and use the Android Studio shortcut Ctrl Shift T (as far as not overwritten by another shortcut configuration in your IDE). Don’t forget to enable JUnit4 as your preferred testing library.

CreateTestInJUnit4

Create Test JUnit 4

Moreover, Android Studio instantly auto-creates an appropriate JUnit run configuration. If it did not, it has to be configured as follows:

JUnitConfiguration

Run/Debug Configurations JUnit

Keep in mind that there is a fundamental difference between Android Tests run configurations, appropriate for integration tests, and JUnit run configurations!

Test Class

A Robolectric test class can look like the following one:


Syntactically, you have to take care of the following configuration parameters.

@RunWith(RobolectricGradleTestRunner)

To apply Robolectric within a Gradle project, a RobolectricGradleTestRunner is required, comparable to MockitoJUnitRunner etc. If you are using Maven as build management tool, RobolectricTestRunner.class has to be used.

@Config(constants, sdk)

The annotation field constants requires a project’s BuildConfig.class, e.g. to access widgets’ resource ids etc.

In case you want to use another SDK version due to compatibility tests or you have to use another SDK because Robolectric does not yet support it, it can be manually specified.

Gradle Run Configuration

Additionally, all tests can be executed by a proper Gradle run configuration.

GradleConfiguration

Run/Debug Configurations Gradle

Continuous Integration

Last but not least, tests can easily be executed on your build environment by using Gradle.

Summary

Finally, by reading this blog-post, you are capable of

  • adding Robolectric to a Gradle Android Studio project
  • preparing your project structure as far as required
  • creating your first test class
  • running one or all tests from Android Studio by your JUnit or Gradle run configurations
  • executing unit tests by command prompt

Hope you enjoyed it and brace yourselves for my next blog posts about Robolectric.

— Stefan

Comment article

Comments

  1. Testing with Robolectric - Flavors - NovaTec Blog

    […] 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. […]

  2. Testing with Robolectric - Flavors - NovaTec Blog

    […] 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. […]