Follow treslines by email clicking Here!

Saturday, April 27, 2013

Testing Android Apps with Junit (no more slow emulator)

Hi there, today i wanna share a very helpful way to test our Android Apps with fast Junit tests instead of using the "slow emulator". With Junit you'll speed up your development and ensure quality.

  • Your Project must use at a minimum platform 2.2 Google API level 8. (This is the most cases)
  • You must use Junit 4 Library and not Junit 3.
  • Download the newst jar file from here: robolectric-X.X.X-all.jar 
How to do it? 
  1. Create your Android project normally.
  2. select your project and add a new folder called test like the pic bellow.

Creating a Junit Test Project
Create a simple Java-Project (attention not another Android-Project) with the same name as your Android project but with the suffix -Test at the end and click next. (Attenion: do not press finish)  
Ex: MyProject and MyProjectTest

Select the src-folder, right mouse click and select "remove source folder" from build path. (the src folder will change from source into normal folder. see picture bellow after point 4)

Select the src-folder, right mouse click and take "Link additional source" and browse the created test folder from your Android Project and then click finish. The result will be something like point 1 in the pic above.

Create a folder called lib in your test project like point 4 in the picture above and copy/paste the downloaded roboelectric-x.x.x-all.jar into it.

Now select your test project, right mouse click, select Build Path > Configure Build Path... > select tab libraries > add library > Junit > next > select Junit 4 > click finish but stay in this dialog.

Click add jars... > select your test project (in this example here: MyProjectTest) > lib > select roboelectric-x.x.x-jar > click ok but stay in this dialog. (see picture bellow point 1)

Click add external jars... > [browse the location from your android-sdk] > platforms > android 8 > select android.jar > click ok but stay in this dialog. (see picture bellow point 1)

Click add external jars... > [browse the location from your android-sdk] > add-ons> addons-google_apis-android-8 > libs > select maps.jar > click ok. (see picture bellow point 1)

Select the Tab Projects > add your test project to it like the picture bellow. After that press the refresh key (F5) and run a project clean. (Project > clean...) (see picture bellow)

Setup your test run configuration
Attention: This step is very important. It will not work, if you don't do it.

Select menu run > run configurations... > double click Junit > and follow the steps in the picture bellow:

Write your first test to validate all those steps
Go back to your test project (in this case here MyProjectTest) and create a simple class in the test folder called MyActivityTest and copy the next code lines into it.

package com.example;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.example.myproject.MainActivity;
import com.example.myproject.R;
import com.xtremelabs.robolectric.RobolectricTestRunner;

public class MyActivityTest {

    public void shouldHaveHappySmiles() throws Exception {
        String hello = new MainActivity().getResources().getString(R.string.hello_world);
        assertThat(hello, equalTo("Hello world!"));

Run the test
Menu > Run > Run Configurations... > select Junit > select your created MyProjectTestConfiguration and run it. If everything went well, we should get something like this:

So bye bye slow emulator... ;-) happy coding!


  1. Great article!

    Just one thing: you should change robolectric-X.X.X-all.jar to robolectric-X.X.X-jar-with-dependencies.jar since robolectric-X.X.X-all.jar is the old way.

  2. Hi magnus!

    Thanks! The reason i choose [robolectric-X.X.X-all.jar] and not [robolectric-X.X.X-jar-with-dependencies.jar] was because the [robolectric-X.X.X-jar-with-dependencies.jar] didn't work for me. i've tryed sereval times with while with [robolectric-X.X.X-all.jar] works fine.

    As soon as i get it working in my environment i'll update it here.


  3. This comment has been removed by the author.

  4. Is it possible to implement a jUnit test using Roboelectric for Hybrid Mobile applications developed in jQuey Mobile and cordova?

  5. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Mobile App Development Company
    Mobile App Development Companies

  6. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...

    Android App Development Company

  7. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

  8. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Fitness SMS
    Salon SMS
    Investor Relation SMS

  9. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    iOS App Development Company

  10. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

  11. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..