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 .
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 .
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...... :)