Easy Steps to use Robotium and Junit in Android Studio for app Unit Testing


Today I will show you how to do unit testing of android code with Robotium JUnit in android studio in very easy steps
While I was testing my app I came across that most of the testing done by the people is with Robotium.The testing code is also available on net, but most of it is on eclipse environment and not on studio and in their tutorials they have said how to add robotium library in eclipse but it was difficult add in studio and other problem is how to import gradle studio project in eclipse most of time it gives error. so I search out many post and comments in stackoverflow and other website and i came to know this steps, with this you can easily do testing of same app in android studio in same file and no need to create new project like we have to do in eclipse.
Not wasting so much of time 
So here we go  

Video tutorial 




Download Robotium 5.3.1 jar file size 117kb


Copy the jar file in app/libs folder




Now in android studio expand Gradle scripts
  click/open -> build.gradle(Module: app) 


 In dependencies copy and paste below code:
For Robotium  
 dependencies {
   ---
       androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.3.1'
           ---
}

For testing with JUnit 
dependencies {
---
  testCompile 'junit:junit:4.12'
  testCompile "org.mockito:mockito-core:1.9.5"
--
}

Now in MainActivity.java file we will create sample for containing 1 button and 1 Textview and we will test it


MainActivity.java
 package com.ankitban.dev.mytesting;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    private Button btn_click;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_click = (Button) findViewById(R.id.btn_test);
        tv = (TextView) findViewById(R.id.main_tv);
        btn_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("All working");
            }
        });
    }
}


res/layout  activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="12" />

    <TextView
        android:id="@+id/main_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_test"
        android:text="@string/hello_world" />

</RelativeLayout>

Testing code:

Create sample Test file in androidTest folder as shown in image which will test our button and TextView which we have created in MainActivity 



ExampleTest.java

package com.ankitban.dev.mytesting;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.widget.Button;
import android.widget.TextView;


import com.robotium.solo.Solo;

public class ExampleTest extends ActivityInstrumentationTestCase2{

    private Solo solo;

    public ExampleTest() {
        super(MainActivity.class);
    }

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

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

    public void test_MainActivityChangeTextView_hi() throws Exception{
        solo.unlockScreen();
        Button btn_test = (Button) solo.getView(R.id.btn_test);
        solo.clickOnView(btn_test);
        solo.waitForText("hi", 1, 2000);
        TextView tv = (TextView) solo.getView(R.id.main_tv);
        String result = tv.getText().toString();
        assertEquals(result, "All working");
    }

    public void testEquals() throws Exception{
        assertEquals(1, 1);
    }

    public void testBoolean() throws Exception{
        assertTrue(true);
    }
}

now coding part is over 

Its time to run the code

oh wait before running the app we have to change the configuration

Run > Edit Configurations ... > Add New Configuration > Android Tests
Name: Unnamed -> MyUnitTestPlan
Module: -> app
Test: All in Module
Target device: Show chooser dialog
Click OK to save configuration
Connect your test device by USB


Launch you test plan

Run > Run ... > MyUnitTestPlan

Testing Results:




Labels: , , ,