0

Can someone show me a working code to do a unit test on this code?

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
}

Please show me using local unit test(roboelectric.. etc, if possible) and instrumentation test.

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

<Button
    android:id="@+id/login"
    android:text="Login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

Obviously the test will check if there is a button with a text Login created.

Tesla_PH
  • 3
  • 4
  • Did you try to read any documentation? https://developer.android.com/training/testing/start/index.html http://www.vogella.com/tutorials/Robotium/article.html – Miguel Benitez Aug 10 '16 at 09:09

1 Answers1

0

Use Expresso:

if(onView(withText("Login")).exists()){ 
    doSomething(); 
} else { 
   doSomethingElse(); 
}

Here are the details on how to setup using Expresso.

David
  • 15,894
  • 22
  • 55
  • 66