Robotium: black-box testing for Android apps

06 Jul, 2011

As pointed out in an earlier post the importance of testing can not be understated.
In this post we will delve into BDD of Android apps.
There are a number of other testing tools for Android out there, such as Robolectric and Calculon. Robolectric improves the speed of running the test by executing it outside of the emulator. Calculon is a DSL for testing views and activities. As Robotium seems to most mature and reliable, it is my preference.

What is Robotium & what does it do?

Robotium is a test framework created to make it easy to write powerful and robust automatic black-box test cases for Android applications. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.

In essence, a Robotium test is a subclass of junit.framework.TestCase (an ActivityInstrumentationTestCase2, in this “case”) in which, by use of the Robotium library, a Solo object is created which allows easy access to the views in your activities.
Through the solo object, you can set values in input fields, click on buttons and get results from other UI components. Methods of JUnits Assert class can then be used to check those results.
How to set up Robotium?
After creating an Android Test project in the usual way (I will assume you have done that before), you need to add the robotium JAR to the Libraries on the projects Build Path.

How to use Robotium?
In the setUp method of your testing class, you can create a Solo object and specify the Activity to be started.
The Solo object can then be used in your testXXX methods to get and set values in/from UI components and click buttons and such.
Many of Solos methods to access UI components take an int representing the index of the component in the UI. This can lead to brittle code so using getView (and the id defined in the layout xml) is a much safer alternative.
Do keep in mind that changing values and performing clicks is not allowed from outside of the thread that originally created the views, these things must still be done through Solos methods such as setXXX, EnterText and clickOnXXX.
The values to be asserted can then be retrieved through Solos methods isXXX, getCurrentXXX, searchText and others.
Solos finalize method should be called from the tests tearDown method.
An example:
[sourcecode language=”java”]
package xebia.addapptesting;
import xebia.addapp.AddApp;
import xebia.addapp.R;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
public class AddTest extends ActivityInstrumentationTestCase2<AddApp> {
private Solo solo;
public AddTest() {
super("xebia.addapp", AddApp.class);
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
public void testAdd() {
int one = 1;
int other = 2;
solo.enterText((EditText) solo.getView(, Integer.toString(one));
solo.enterText((EditText) solo.getView(, Integer.toString(other));
assertTrue(solo.searchText(Integer.toString(one + other)));
public void tearDown() throws Exception {
try {
} catch (Throwable e) {
When running the test case on the emulator or an actual device, you will see the values being entered into the UI components or see them being clicked as if you were doing it yourself. After the tests are completed, the JUnit view in Eclipse will show which tests have failed and passed.

I think it looks pretty cool to see the app being run and the values being entered. This kind black-box behavior testing on the emulator is much more realistic than just a unit test.
Stay tuned for my next post on a surprise topic, soon!

Newest Most Voted
Inline Feedbacks
View all comments
10 years ago

Can you please explain the code.. I am a android tester.. it will be useful for me..
My mail id is:

piyush jhawar
piyush jhawar
10 years ago

1. can u plz explain how can i make test cases in Robotium ?
2. i have a project source code now how can i test my app ?
i tried learning Robotium from 10 days, but i am unable to understand.plz help. i am very confused.

9 years ago
Reply to  piyush jhawar

I myself browsing it too from last week ,, if in case you got to learn it ,, please help me out too.

piyush jhawar
piyush jhawar
10 years ago

My mail id is


[…] Package Synergy November 14, 2011By Arno den HondUnlike announced in my previous post this one is neither soon nor on a surprise topic. It is about a general aspect of Android that is, […]

10 years ago

Very informative post. Its really helpful for me and beginner too. Check out this link too its also having a nice post with wonderful explanation on black box testing….

6 years ago
Reply to  PraveshSingh

Would you elaborate on how to write test cases for an apiatclpion having multiple activities/services. Should we extend the test case classes from android.test.ActivityInstrumentationTestCase2 or android.test.InstrumentationTestCase? An example would be very handy. Thank you

10 years ago

Hi all,
I try to run robotium from the ubuntu terminal and its works fine, but now i want to pass the arguments (like, wants to run the test 100 times or 200 times) at the time of running from the command line(terminal). How can i do that using ubuntu terminal??

10 years ago

We need source code of apk file which we want to automate for using
code in our robotium tests.

10 years ago

Could you please explain how the robotium to test an apk when does not the source code of the apk?

Explore related posts