【问题标题】:How to test the textbox value in the Android JUnit test如何在 Android JUnit 测试中测试文本框的值
【发布时间】:2011-03-26 07:12:58
【问题描述】:

我刚刚创建了登录屏幕项目。我为此编写了测试用例。我不知道如何在 JUnit 测试用例的文本框中输入文本。我只是喜欢这个

         public void testlogin() {
    final String n1 = "adithi";
    final String p1 = "adithi";
    String name, pass;
    editUname.clearComposingText();
    editPswd.clearComposingText();
    TouchUtils.tapView(this, editUname);
    sendKeys("adithi");
    TouchUtils.tapView(this, editPswd);
    sendKeys("adithi");

    activity.runOnUiThread(new Runnable() {

          public void run() {


            signinbtn.performClick();
          }
        }); 

        name = editUname.getText().toString();

        pass = editPswd.getText().toString();

    Log.e("name",name); 
    Log.e("Password",pass);
    assertEquals(n1, name);
    assertEquals(p1, pass);
}

测试用例结果是

 junit.framework.ComparisonFailure: expected:<adithi> but was:<>
 at com.firstpageTest.Test.testlogin(Test.java:126)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
 at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
 atandroid.test.ActivityInstrumentationTestCase2.runTest
    (ActivityInstrumentationTestCase2.ja
 atandroid.test.ActivityInstrumentationTestCase2.runTest
   (ActivityInstrumentationTestCase2.java:186)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

请指导我。

【问题讨论】:

  • 天哪!您已经编辑了问题并更改了代码!在您performClick() 之后,我可以在某处确认 UI 文本是否为空

标签: java android unit-testing junit testcase


【解决方案1】:

这是一个简单的问题,我在向 JUnit 提供输入时犯了一个小错误。我们必须提供为

       sendKeys("A D I T H I");

现在可以了。

【讨论】:

    【解决方案2】:

    注意。我不是 Java 人,但..

    你的测试失败了,因为它失败了,你的代码没有像预期的那样运行。它说expected:&lt;Karthika&gt; but was:&lt;&gt;

    所以你的用户界面中的字符串可能是null。您将字符串 Karthika 放在 UI 中,然后您的测试期望两个字符串都是 adithi,但它却说它为空。

    1. 您的 UI 中的某处文本变为空。
    2. 认为应该是assertEquals(actualvar, whatyouexpect);,比如assertEquals(name, n1);

    【讨论】:

    • 我查过了,它是assertEquals(String expected,String actual);
    • 它会在某处变为空或空白。 testlogin 何时/如何运行? performclick() 是做什么的?
    • 我认为 sendkeys("") 不是输入文本框值的方法。但我无法猜测给出的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2020-12-07
    • 2012-11-27
    相关资源
    最近更新 更多