Android TDD using Robolectric

robolectric

 

Robolectric is a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds.

Robolectric allows a test style that is closer to black box testing, making the tests more effective for refactoring and allowing the tests to focus on the behavior of the application instead of the implementation of Android.

Robolectric makes this possible by rewriting Android SDK classes as they’re being loaded and making it possible for them to run on a regular JVM.

A Step by Step walkthrough of the setup

1. Create a simple android application project and name it as “DemoApp”.

2. Under libs folder of the android project add a new folder named test and paste the robolectric-2.2-jar- with-dependencies.jar jar file into it. The reason we use a simple folder and not a source folder is, we do not want our robolectric jar file to compile into the project. So creating a simple folder is a right option. The robolectric-2.2-jar-with-dependencies.jar contains all the necessary dependency classes we require to write our test cases.

3. Next create a simple folder named test into ROOT of your project. This folder will consist of all our classes containing test cases.

4. Create a simple Java Project into Eclipse named “DemoAppTest”. This project is where we would write our test cases for our main project i.e. “DemoApp”.

5. Now while creating the Java project, we need to set few things up in the wizard that is opened up in front of you,

  • Link your test folder from the main project to your Java project, so that your Java project has a source folder linked to your main project. To do this, Click on to the “Link additional source” link and give the path to “test” folder from the main project.
  • On the “Projects” tab of the wizard click add to add an existing project (DemoApp) to your Java Project.
  • On the “Libraries” tab of the wizard first add a jar robolectric-2.2-jar-with-dependencies.jar from the main project ((DemoApp). Next add an external jar (as shown in the video locate the android.jar on your PC). We need this jar because, we need our test project to work as android test project even though its a Java Project. We are writing test cases for an android project so this is required.
  • Next add a library. Here we need to select “JUnit 4” since robolectric does not support JUnit 3. This library is required to run our test cases.

6. Now your test project is ready, before we run it we have to configure RUN setup. To do this go to Run as—>Run Configurations. On the wizard that you see, double click on JUnit on the left, and select your test project.

7. On the right panel, below the “Test” tab, choose the “Run all tests in the selected project, package or source folder”, and select your Java Test Project. This ensures all your test classes run at the same time.

  • For “Test runner” option, only select JUnit 4 and not JUnit 3, as Robolectric only supports for JUnit 4.
  • For “Multiple Launchers available” option, select “Eclipse JUnit Test Launcher” and not “Android JUnit Test Launcher”, the reason being we need eclipse JUnit perspective while running our test cases.

8. Now under Arguments tab, in “Working Directory” select “Other”, and navigate to your main android project (Demo App), it will set the string like ${workspace_loc: Your_App_Name}. This is needed to tell which android project from the workspace we need to work with. With this we are done with the entire setup part on the eclipse side.

9. As shown in the video create a new JUnit Test class and run your test project as instructed.


Note:
The first time you do Step 9 before the test projects executes, eclipse will download few maven dependancies for the test project to actually run. This happens
ONLY ONCE, so no need to worry about.

Now you have your setup ready to start writing test cases for your android project..