Friday 22 March 2013

Android Unit Testing With Robolectric | Android TDD | Android Unit Test Case

Hello Droid Guys,
           This is my first android Hello World test project using Roboelectric.
This tutoial covers  following testing topic.
                                                                                 

1. How to test text on a TextView using Roboelectric in Android.                          
2. How to test image on a ImageView using Roboelectric in Android
3. How to test New Activity call using Roboelectric in Android
4. Testing Button is visible or not in Android using Roboelectric
5. Test case for button click in android using Roboelectric
5. How to check intent using Roboelectric in Android
6. How to make API call using Roboelectric in Android
7. Calling HTTP request in Android Roboelectric test project

Before starting writing the above listed test case , If you are thinking about how to setup
your First android test project , then see the Robolectric Setup  tutorial .

First of all I am going to create an android Project "RoboElectric" .

Here is my Android Code:

1. MainActivity.java :
package com.example.roboelectric;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 Button login;
 EditText name;
 EditText password;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  login = (Button) findViewById(R.id.login);
  name = (EditText) findViewById(R.id.name);
  password = (EditText) findViewById(R.id.password);

  login.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent home = new Intent(MainActivity.this, Home.class);
    startActivity(home);
   }
  });

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}
2. Home.java
package com.example.roboelectric;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Home extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_home);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_home, menu);
  return true;
 }

}
3. Second.java
package com.example.roboelectric;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Home extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_home);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_home, menu);
  return true;
 }

}
4. AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.roboelectric"
    android:versioncode="1"
    android:versionname="1.0" >

  <uses-sdk
        android:minsdkversion="8"
        android:targetsdkversion="16" >

        <application
            android:allowbackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >

            <activity
                android:name="com.example.roboelectric.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                  <action android:name="android.intent.action.MAIN" >
                     <category android:name="android.intent.category.LAUNCHER" >
                     </category>
                   </action>
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.roboelectric.Home"
                android:label="@string/title_activity_home" >
            </activity>
          <activity
                android:name="com.example.roboelectric.Second"
                android:label="@string/title_activity_second" >
            </activity>
        </application>
  </uses-sdk>
</manifest>

What Are the expected test case for above android project ?? 

 1. checking hello world text
 2. login button is visible or not
 3. test startActivity() starts a new activity or not on click of login button '
 Now, I am Going to write my test Project "RoboElectricTest"

1.MyActivityTest.java
import java.io.IOException;

import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.matchers.StartedMatcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.roboelectric.Home;
import com.example.roboelectric.MainActivity;
import com.example.roboelectric.R;
import com.example.roboelectric.Second;

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

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

 private MainActivity activity;
 private Button login;
 
 @Before
 public void setUp() throws Exception {
   activity = new MainActivity();
   activity.onCreate(null);
   login = (Button) activity.findViewById(R.id.login);
 }
 
  // checking hello world text
 @Test 
 public void shouldHaveHappySmiles() throws Exception {
  String hello = new MainActivity().getResources().getString( R.string.hello_world);
  assertThat(hello, equalTo("Hello world!"));
 }   
 // Button visible 
 @Test 
 public void testButtonsVisible() { 
  assertThat(login.getVisibility(), equalTo(View.VISIBLE)); 
 } 
 
 // startnew activty 
 @Test 
 public void shouldStartNextActivityWhenButtonIsClicked() { 
  login.performClick();
  assertThat(activity, new StartedMatcher(Home.class));
  }
}


Hope this will helps you ,in writing UI test case for your android project using Roboelectric .

Enjoy Coding. Cheers...... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

6 comments:

  1. Hi,
    Loved your link on first test project.
    But i get this: The import org.robolectric cannot be resolved.

    ReplyDelete
  2. Error:
    The import org.robolectric cannot be resolved

    To fix I Replace:
    import org.robolectric.Robolectric;
    import org.robolectric.RobolectricTestRunner;
    import org.robolectric.matchers.StartedMatcher;
    With:
    import com.xtremelabs.robolectric.RobolectricTestRunner;

    Fixed

    ReplyDelete
  3. Hey Thanks,
    Sorry for late response.Actually, you are using roboelectric older version so its giving you the import error. Try with robeoelectric-2.0-alpha jar , the xtremelabs.robolectric (the version you are using)...not supports the layout inflator,listview and adapter test.

    Try with robeoelectric-2.0-alpha.jar this will resolve your import error.

    ReplyDelete
  4. Hi can you please help with a question on stackoverflow? Thank you

    http://stackoverflow.com/questions/19471704/roboelectric-2-2-error

    ReplyDelete
  5. Thanks for the tutorial. Love to see a more advanced version doing stuff like setting application state, etc.

    ReplyDelete
  6. Iam a beginner on android...i feel its a nice tutorial..Please give me a complete idea in doing multiscreen support for an android application

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.