Skip to content

Ex1. Test click button start another activity

tyanhly edited this page Apr 23, 2013 · 4 revisions

Introduction

This tip show you the steps to create a simple test for beginner. When I compose this tip, I hope i can write a basic knowledge about robolectric. So please contact me if this tip is wrong.

Environment

OS: linux java: 1.7 IDE: eclipse robolectric 2.0 alpha 2

Step 1. Create android application by eclipse

    Application Name a1
    Project name a1
    Package: com.example.a1

Step 2. Create another activity

Create AnotherActivity.java in com.example.a1 package
public class AnotherActivity  extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.another);
    }
}

Create another.xml in res/layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Another activity"
        android:gravity="center"
    />
</linearlayout>

Step 3. Coding for main screen

Edit MainActivity.java
package com.example.a1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(AnotherActivity.class);
            }
        });
        
    }
    
    private void startActivity(Class<? extends Activity> activityClass) {
        Intent intent = new Intent();
        intent.setClassName(getPackageName(), activityClass.getName());
        startActivity(intent);
    }
}

Edit layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblBtn"
    />
</linearlayout>

Step 4. Create Test project

Create test folder in root project path.
Create package com.example.a1 in test folder.
Create MainActivityTest class in this package.
package com.example.a1;

import static org.robolectric.Robolectric.shadowOf;
import static org.junit.Assert.assertEquals;

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

import android.content.Intent;
import android.widget.Button;

import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowIntent;

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
    MainActivity mainActivity;

    @Before
    public void setup(){
        mainActivity = new MainActivity();
    }

    @Test
    public void testButtonClickShouldStartAnotherActivity() throws Exception{
        mainActivity.onCreate(null);
        Button btn = (Button) mainActivity.findViewById(R.id.btn);
        btn.performClick();
        
        ShadowActivity shadowActivity = shadowOf(mainActivity);
        Intent intent = shadowActivity.getNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(intent);
        assertEquals(shadowIntent.getComponent().getClassName(), AnotherActivity.class.getName());
    }
}

Step 5. Configuration and run

 - Configurate build path
   - Right click a1 project
   - Select "Refesh"
   - Right click a1
   - Select "Build Path" => "Configure Build Path..."
   - Add JUnit library on library tab
   - Add robolectric-X.X.X-jar-with-dependencies.jar (remember! - using dependencies for this tutor)
   - Add android.jar from <your android install directory>/platforms/android-17/android.jar
   - Add maps.jar from <your android install directory>/add-ons/addon_google_apis_google_inc_17/libs/maps.jar
 - Configurate and run test unit
   - Go to "Run" => "Run Configuration"  
   - Double click "JUnit" (not "Android JUnit Test")
   - Name: a1-test
   - Select the "Run all tests in the selected project, package or source folder:" radio button
   - Click the "Search" button
   - Select "a1"
   - TestRunner: JUnit 4
   - Click on the link "Multiple launchers available Select one…" at the bottom of the dialog
   - Check the “Use configuration specific settings” box
   - Select "Eclipse JUnit Launcher"
   - Click "OK"
   - Click "Run"

Source code

https://github.com/tyanhly/a1.git