How to call Button.performClick in Android JUnit test case?

I am new to Android testing. I would like to check whether pressing a button will open the corresponding operation or not. I did some research and found out that I would need to use the ActivityManager for validation.

The problem is that I cannot get the “clicked” part to work. I am trying to use Button.performClick().

At first, I just called this function and got an error stating that I could not do this in the current thread. After some googling, I found out that I needed to call it in the UI thread and came across runOnUiThread(Runnable r) method.

The button I'm trying to click is _helloButton_. It turns in _setUp()_ method. I checked _assertNotNull_to verify this.

As part of the test method, I call

mActivity.runOnUiThread(new Runnable() {
        public void run() {
            helloButton.requestFocus();
        }
    });
helloButton.performClick();

and I get NPE in the line calling requestFocus().

Next I tried

mActivity.runOnUiThread(new Runnable() {
        public void run() {
            helloButton.performClick();
        }
    });

and still get the same null pointer exception.

In JUnit perspective, I get this message

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details

And stackTrace looks like this.

    08-05 19:03:11.922: ERROR/AndroidRuntime(578): Uncaught handler: thread main exiting due to uncaught exception
08-05 19:03:11.922: ERROR/AndroidRuntime(578): java.lang.NullPointerException
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.example.helloworldmk2.test.HelloWorldMK2Test$1.run(HelloWorldMK2Test.java:57)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Handler.handleCallback(Handler.java:587)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Looper.loop(Looper.java:123)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invoke(Method.java:521)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at dalvik.system.NativeStart.main(Native Method)

Line 57 is where I call helloButton.performClick().

I am not sure why I get NPE; assertNotNull passes without problems. If you can help me with this, I would really appreciate it. Thanks in advance.

EDIT: I will subclass ActivityInstrumentationTestCase2 for this particular test class.

EDIT2: Logcat throws up some errors before NPE.

I see

08-05 20: 08: 54.702: ERROR / AndroidRuntime (754): ERROR: thread failure

and

08-05 20: 08: 58.642: ERROR / gralloc (52): [unregister] handle 0x3e1b28 is still locked (state = 40000001)

+5
3

:

@UiThreadTest
public void testNoErrorInCapitalization() {
    final String msg = "this is a sample";
    mMessage.setText(msg);
    mCapitalize.performClick();
    final String actual = mMessage.getText().toString();
    final String notExpectedRegexp = "(?i:ERROR)";
    assertNotContainsRegex("Capitalization found error:",
        notExpectedRegexp, actual);
}

, Android.

+15

runTestOnUiThread(),

public void testHelloButton() throws Throwable{
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                helloButton.performClick();
            }
        });

    }
0

... , , BEWARE, sudo-

public void myOnClickListener(View source) {

  switch source.getId() {
  case R.id.Your_Button_Name_In_Xml :
    Intent MyNewActivity = new Intent(this, MyNewClass.class);
    startActivity(MyNewActivity);
  }
}

http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html

-1

All Articles