如何调用Button.performClick在Android的JUnit测试案例? [英] How to call Button.performClick in Android JUnit test case?

查看:651
本文介绍了如何调用Button.performClick在Android的JUnit测试案例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid测试的东西。我想测试,如果点击一个按钮将打开一个相应的活动或没有。我做了一些研究,发现我将需要使用ActivityManager做检查。

I'm new to Android Testing stuff. I'd like to test if clicking a button will open up a corresponding activity or not. I did some research and found out that I will need to use ActivityManager to do the checking.

但问题是,我不能让点击的一部分工作。我试图使用 Button.performClick()

Problem is, I can't get the "clicking" part working. I'm trying to use Button.performClick().

起初,我只是调用该函数,并得到了一个错误,说我不能在当前线程中执行此操作。一些谷歌上搜索后,我发现我需要调用它的UI线程,和碰到 runOnUiThread(Runnable的R)方法

Initially I just called this function, and got an error saying that I can't do this in the current thread. After some googling I found out that I need to call it in the UI thread, and came across runOnUiThread(Runnable r) method.

按钮我想点击的 _helloButton _ 。这是在获得_setup()方法_ 。我做了 _assertNotNull _ 检查是为了确保它的存在。

The button I'm trying to click is _helloButton_. This is obtained in the _setUp()_ method. I did _assertNotNull_ check on this to make sure it's there.

在一个测试方法,我称之为

Within a test method, I call

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

和我得到一个NPE在排队叫号不是requestFocus()

and I get a NPE at the line calling requestFocus().

接下来我想

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

和仍然得到同样的空指针异常。

and still get the same null pointer exception.

在JUnit的角度来看,我得到这个消息

On the 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

和堆栈跟踪是这样的。

    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)

57号线在这里我打电话了 helloButton.performClick()

我不知道为什么我收到NPE; assertNotNull通过没有问题。如果你能帮助我解决这个问题,我真的AP preciate它。先谢谢了。

I'm not sure why I'm getting NPE; assertNotNull passes without problem. If you can help me with this issue, I'd really appreciate it. Thanks in advance.

编辑:我继承ActivityInstrumentationTestCase2这个特定测试类

I'm subclassing ActivityInstrumentationTestCase2 for this particular test class.

EDIT2:logcat的喷出了一些错误,NPE发生之前

Logcat spews out some errors before NPE happens.

我看

08-05 20:08:54.702:ERROR / AndroidRuntime(754):错误:线程附加失败。

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

08-05 20:08:58.642:ERROR / gralloc(52):[注销]处理0x3e1b28仍处于锁定状态(状态= 40000001)

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

推荐答案

您可以标注您的测试,UI线程上运行:

You can annotate your test to run on UI thread:

@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应用程序测试指南。

这篇关于如何调用Button.performClick在Android的JUnit测试案例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆