Robotium Jump Start PRO

The Robotium framework provides us with testing automation for the android platform. It supports testings both for native and for hybrid applications. Using the Robotium framework we can easily write our own automatic black-box test cases.

The following is the code of the activity I test in the video clip.

package com.lifemichael.samplecalc;

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

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final EditText text1 = (EditText) findViewById(R.id.editText1);
		final EditText text2 = (EditText) findViewById(R.id.editText2);
		final EditText text3 = (EditText) findViewById(R.id.editText3);
		Button btPlus = (Button) findViewById(R.id.button1);
		Button btMinus = (Button) findViewById(R.id.button2);
		btPlus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				double num1 = Double.parseDouble(text1.getText().toString());
				double num2 = Double.parseDouble(text2.getText().toString());
				text3.setText(String.valueOf(sum(num1, num2)));

			}
		});
		btMinus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				double num1 = Double.parseDouble(text1.getText().toString());
				double num2 = Double.parseDouble(text2.getText().toString());
				text3.setText(String.valueOf(difference(num1, num2)));

			}
		});

	}

	protected double sum(double num1, double num2) {
		return num1 + num2;
	}

	protected double difference(double num1, double num2) {
		return num1 - num2;
	}

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

}

The following is the code that tests the activity we developed.

package com.lifemichael.samplecalc.test;

import com.lifemichael.samplecalc.MainActivity;
import com.jayway.android.robotium.solo.Solo;
import com.lifemichael.samplecalc.R.*;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.EditText;

public class MainActivityTest extends
		ActivityInstrumentationTestCase2<MainActivity> {

	private Solo solo;

	public MainActivityTest() {
		super(MainActivity.class);
	}
  
	public void testPlusOperation() throws Exception {
		Log.i("mytest","inside testPlusOperation");
		EditText firstEditText = (EditText) solo.getView(com.lifemichael.samplecalc.R.id.editText1);
		solo.enterText(firstEditText, "3");
		EditText secondEditText = (EditText) solo.getView(com.lifemichael.samplecalc.R.id.editText2);
		solo.enterText(secondEditText, "4");
		solo.clickOnButton("plus");
		EditText thirdEditText = (EditText) solo.getView(com.lifemichael.samplecalc.R.id.editText3);
		String result = thirdEditText.getText().toString();
		Log.i("mytest","result="+result);
		assertEquals(result,"7.0");
		
	}

	public void setUp() throws Exception {
		solo = new Solo(getInstrumentation(), getActivity());
	}

	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
	}
}

You can find the complete sample including all files as well as slides and video clips for learning how to use this testing framework in my new Robotium Basics free course at http://abelski.lifemichael.com.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences