1.导入一个项目工程。
package com.calculator;import com.calculator.R;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.text.Editable;public class Main extends Activity { EditText vFirstValue; EditText vSecondValue; TextView vResult; Button vCalculate; float vNum1 , vNum2, vOutPut; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); vFirstValue = (EditText) findViewById(R.id.EditText01); vFirstValue.setText("0"); vFirstValue.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { vFirstValue.setText(""); return false; } }); vSecondValue = (EditText) findViewById(R.id.EditText02); vSecondValue.setText("0"); vSecondValue.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { vSecondValue.setText(""); return false; } }); vResult = (TextView) findViewById(R.id.TextView01); vResult.setText("0.00"); vCalculate = (Button) findViewById(R.id.Button01); //Adding listener to button vCalculate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //Getting first & second values and passing to show result showResult(vFirstValue.getText().toString(), vSecondValue.getText().toString()); } }); } //Showing multiply results public void showResult(String vFirst, String vSecond) { vNum1 = Float.parseFloat(vFirst); vNum2 = Float.parseFloat(vSecond); vOutPut = vNum1 * vNum2; vResult.setText(String.valueOf(vOutPut)); } }
2.项目源码下载地址 :https://Robotium.googlecode.com/files/AndroidCalculator.zip
3.新建一个被测试的程序。
package com.calculator.test;import android.test.ActivityInstrumentationTestCase2;import android.widget.EditText;import android.widget.TextView;import com.calculator.Main;import com.robotium.solo.Solo;import com.calculator.R;@SuppressWarnings("rawtypes")public class MainTest extends ActivityInstrumentationTestCase2 { private Solo solo; @SuppressWarnings("unchecked") public MainTest() { super(Main.class); // TODO Auto-generated constructor stub } @Override protected void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); } public void testDisplayWhiteBox() { float vFirstNumber = 10; float vSecondNumber = 10; float vResult = vFirstNumber * vSecondNumber; EditText vFirstEditText = (EditText) solo.getView(R.id.EditText01); solo.clearEditText(vFirstEditText); solo.enterText(vFirstEditText, String.valueOf(vFirstNumber)); // AccessSecondvalue(edit-filed)andputtingSecondNumbervalueinit EditText vSecondEditText = (EditText) solo.getView(R.id.EditText02); solo.clearEditText(vSecondEditText); solo.enterText(vSecondEditText, String.valueOf(vSecondNumber)); // ClickonMultiplybutton solo.clickOnButton("Multiply"); assertTrue(solo.searchText(String.valueOf(vResult))); TextView outputField = (TextView) solo.getView(R.id.TextView01); // Assert to verify result with visible value assertEquals(String.valueOf(vResult), outputField.getText().toString()); } @Override protected void tearDown() throws Exception { solo.finishOpenedActivities(); }}
4.运行android Junit test!
5.记到导入Robotium包,下载地址:http://pan.baidu.com/s/1dE75v25 密码vrkz