. f Android Unit Testing With Robolectric | Android TDD | Android Unit Test Case ~ Android Developers Blog

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 :
  1. package com.example.roboelectric;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.  Button login;  
  15.  EditText name;  
  16.  EditText password;  
  17.   
  18.  @Override  
  19.  public void onCreate(Bundle savedInstanceState) {  
  20.   super.onCreate(savedInstanceState);  
  21.   setContentView(R.layout.activity_main);  
  22.   
  23.   login = (Button) findViewById(R.id.login);  
  24.   name = (EditText) findViewById(R.id.name);  
  25.   password = (EditText) findViewById(R.id.password);  
  26.   
  27.   login.setOnClickListener(new OnClickListener() {  
  28.   
  29.    @Override  
  30.    public void onClick(View v) {  
  31.     Intent home = new Intent(MainActivity.this, Home.class);  
  32.     startActivity(home);  
  33.    }  
  34.   });  
  35.   
  36.  }  
  37.   
  38.  @Override  
  39.  public boolean onCreateOptionsMenu(Menu menu) {  
  40.   // Inflate the menu; this adds items to the action bar if it is present.  
  41.   getMenuInflater().inflate(R.menu.activity_main, menu);  
  42.   return true;  
  43.  }  
  44.   
  45. }  
2. Home.java
  1. package com.example.roboelectric;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class Home extends Activity {  
  8.   
  9.  @Override  
  10.  protected void onCreate(Bundle savedInstanceState) {  
  11.   super.onCreate(savedInstanceState);  
  12.   setContentView(R.layout.activity_home);  
  13.  }  
  14.   
  15.  @Override  
  16.  public boolean onCreateOptionsMenu(Menu menu) {  
  17.   // Inflate the menu; this adds items to the action bar if it is present.  
  18.   getMenuInflater().inflate(R.menu.activity_home, menu);  
  19.   return true;  
  20.  }  
  21.   
  22. }  
3. Second.java
  1. package com.example.roboelectric;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class Home extends Activity {  
  8.   
  9.  @Override  
  10.  protected void onCreate(Bundle savedInstanceState) {  
  11.   super.onCreate(savedInstanceState);  
  12.   setContentView(R.layout.activity_home);  
  13.  }  
  14.   
  15.  @Override  
  16.  public boolean onCreateOptionsMenu(Menu menu) {  
  17.   // Inflate the menu; this adds items to the action bar if it is present.  
  18.   getMenuInflater().inflate(R.menu.activity_home, menu);  
  19.   return true;  
  20.  }  
  21.   
  22. }  
4. AndroidManifest.xml
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.roboelectric"  
  3.     android:versioncode="1"  
  4.     android:versionname="1.0" >  
  5.   
  6.   <uses-sdk  
  7.         android:minsdkversion="8"  
  8.         android:targetsdkversion="16" >  
  9.   
  10.         <application  
  11.             android:allowbackup="true"  
  12.             android:icon="@drawable/ic_launcher"  
  13.             android:label="@string/app_name"  
  14.             android:theme="@style/AppTheme" >  
  15.   
  16.             <activity  
  17.                 android:name="com.example.roboelectric.MainActivity"  
  18.                 android:label="@string/app_name" >  
  19.                 <intent-filter>  
  20.                   <action android:name="android.intent.action.MAIN" >  
  21.                      <category android:name="android.intent.category.LAUNCHER" >  
  22.                      </category>  
  23.                    </action>  
  24.                 </intent-filter>  
  25.             </activity>  
  26.             <activity  
  27.                 android:name="com.example.roboelectric.Home"  
  28.                 android:label="@string/title_activity_home" >  
  29.             </activity>  
  30.           <activity  
  31.                 android:name="com.example.roboelectric.Second"  
  32.                 android:label="@string/title_activity_second" >  
  33.             </activity>  
  34.         </application>  
  35.   </uses-sdk>  
  36. </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
  1. import java.io.IOException;  
  2.   
  3. import org.robolectric.Robolectric;  
  4. import org.robolectric.RobolectricTestRunner;  
  5. import org.robolectric.matchers.StartedMatcher;  
  6. import org.junit.Before;  
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9.   
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. import com.example.roboelectric.Home;  
  15. import com.example.roboelectric.MainActivity;  
  16. import com.example.roboelectric.R;  
  17. import com.example.roboelectric.Second;  
  18.   
  19. import static org.hamcrest.CoreMatchers.equalTo;  
  20. import static org.junit.Assert.assertThat;  
  21.   
  22. @RunWith(RobolectricTestRunner.class)  
  23. public class MyActivityTest {  
  24.   
  25.  private MainActivity activity;  
  26.  private Button login;  
  27.    
  28.  @Before  
  29.  public void setUp() throws Exception {  
  30.    activity = new MainActivity();  
  31.    activity.onCreate(null);  
  32.    login = (Button) activity.findViewById(R.id.login);  
  33.  }  
  34.    
  35.   // checking hello world text  
  36.  @Test   
  37.  public void shouldHaveHappySmiles() throws Exception {  
  38.   String hello = new MainActivity().getResources().getString( R.string.hello_world);  
  39.   assertThat(hello, equalTo("Hello world!"));  
  40.  }     
  41.  // Button visible   
  42.  @Test   
  43.  public void testButtonsVisible() {   
  44.   assertThat(login.getVisibility(), equalTo(View.VISIBLE));   
  45.  }   
  46.    
  47.  // startnew activty   
  48.  @Test   
  49.  public void shouldStartNextActivityWhenButtonIsClicked() {   
  50.   login.performClick();  
  51.   assertThat(activity, new StartedMatcher(Home.class));  
  52.   }  
  53. }  

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.