4
public class LoginActivityTest extends ActivityInstrumentationTestCase2<Login> {


        Login mActivity;
        private EditText username;
        private EditText password;

          @SuppressWarnings("deprecation")
        public LoginActivityTest()
          {
              super("com.main.account.Login",Login.class);
          }


          @Override
        protected void setUp() throws Exception {
              super.setUp();
              mActivity = this.getActivity();

              username = (EditText) mActivity.findViewById(R.id.username);
              password = (EditText) mActivity.findViewById(R.id.password);
                      }

          public void testPreconditions() { 
              assertNotNull(username);
              assertNotNull(password);
            }


          public void testText() {
              assertEquals("hello",username.getText());
              assertEquals("hello123", password.getText());
            }

}

我得到的错误是这样的:

04-18 16:03:47.132: I/TestRunner(12376): junit.framework.AssertionFailedError: expected:<hello> but was:<>
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.Assert.fail(Assert.java:47)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.Assert.failNotEquals(Assert.java:282)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.Assert.assertEquals(Assert.java:64)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.Assert.assertEquals(Assert.java:71)
04-18 16:03:47.132: I/TestRunner(12376):    at com.crumbs.main.test.LoginActivityTest.testText(LoginActivityTest.java:46)
04-18 16:03:47.132: I/TestRunner(12376):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 16:03:47.132: I/TestRunner(12376):    at java.lang.reflect.Method.invoke(Method.java:507)
04-18 16:03:47.132: I/TestRunner(12376):    at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
04-18 16:03:47.132: I/TestRunner(12376):    at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
04-18 16:03:47.132: I/TestRunner(12376):    at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.TestCase.runBare(TestCase.java:127)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.TestResult$1.protect(TestResult.java:106)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.TestResult.runProtected(TestResult.java:124)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.TestResult.run(TestResult.java:109)
04-18 16:03:47.132: I/TestRunner(12376):    at junit.framework.TestCase.run(TestCase.java:118)
04-18 16:03:47.132: I/TestRunner(12376):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
04-18 16:03:47.132: I/TestRunner(12376):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
04-18 16:03:47.132: I/TestRunner(12376):    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
04-18 16:03:47.132: I/TestRunner(12376):    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

那我该如何测试呢?如在,我如何设置 EditText 的值,以便我可以在这里测试它?

文档有点分散。

4

5 回答 5

6

那我该如何测试呢?如在,我如何设置 EditText 的值,以便我可以在这里测试它?

虚拟测试 EditText.setText() 方法没有多大意义,但是,如果您坚持,请执行以下操作:

public void testText() {
  // simulate user action to input some value into EditText:
  final EditText mUsername = (EditText) mActivity.findViewById(R.id.username);
  final EditText mPassword = (EditText) mActivity.findViewById(R.id.password);
  mActivity.runOnUiThread(new Runnable() {
    public void run() {
      mUsername.setText("hello");
      mPassword.setText("hello123");
    }
  });

  // Check if the EditText is properly set:
  assertEquals("hello", mUsername.getText());
  assertEquals("hello123", mPassword.getText());
}

希望这可以帮助。

于 2012-04-18T11:30:34.747 回答
1

它工作得很好..它只是说你的测试用例失败了,因为 EditText 中的文本不等于你比较的文本..试试这个/..

     @Override
            protected void setUp() throws Exception {
                  super.setUp();
                  mActivity = this.getActivity();

                  username = (EditText) mActivity.findViewById(R.id.username);
                  password = (EditText) mActivity.findViewById(R.id.password);


                          }

              public void testPreconditions() { 
                  assertNotNull(username);
                  assertNotNull(password);
                }


              public void testText() {
//now that it will pass the test case without Exception..
//but if you want to compare text then you may have to set it in xml and then compare it.. doing it in onCreate() of Login may also work.. 
                  assertEquals("",username.getText());
                  assertEquals("", password.getText());
                }
于 2012-04-18T10:46:58.120 回答
1

这种技术的好处是能够在测试运行时看到正在输入的测试字符串。

此方法会将字符串写入 editText 元素,其中密码是您的测试字符串。它应该被添加到仪器测试类(它扩展了 ActivityInstrumentationTestCase2)

private void addPassword(final String password) {
    final EditText pw = (EditText) activity.findViewById(R.id.someId);
    // Send string input value
    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            pw.requestFocus();
        }
    });
    getInstrumentation().waitForIdleSync();
    getInstrumentation().sendStringSync(password);
    getInstrumentation().waitForIdleSync();
}

然后,您可以使用检索文本

    EditText pw = (EditText)findViewById(R.id.someId);
    final String entry = pw.getText().toString().trim();

并比较它们是否相等

    assertEquals(TEST_STRING, entry);

希望这可以帮助。更多信息在这里http://developer.android.com/training/activity-testing/activity-functional-testing.html

于 2014-10-01T11:52:14.540 回答
0

您可以使用与TouchUtils. 这是我所做的:

使用静态方法创建一个TextUtil类,使其易于使用

package org.dashee.venus;

import android.app.Activity;
import android.widget.TextView;

/**
 * Provide a Text utility where a view text can be changed
 * by running inside a thread
 */
public class TextUtils
{
    public static void setText(Activity activity, final TextView view, final String text)
    {
        activity.runOnUiThread(
                new Runnable()
                {
                    @Override
                    public void run ()
                    {
                        view.setText(text);
                    }
                }
        );
    }
}

然后你可以在你的 testMethod 中调用它

public void testText()
{
    TextView email = this.getActivity().findViewById(R.id.email);
    TextUtil.setText(this.getActivity(), email, "example@hello.com");
    assertEquals("example@hello.com", email.getText());
}

希望这可以帮助

于 2014-08-31T00:42:51.823 回答
0

我知道这是一个非常古老的帖子,这个解决方案怎么样:

 @Before
public void init() {
    MockitoAnnotations.initMocks(this);

    when(rootView.findViewById(R.id.button_logout)).thenReturn(buttonLogout);
    when(rootView.findViewById(R.id.button_unlock)).thenReturn(buttonUnlock);
    when(rootView.findViewById(R.id.ScreenLock_PasswordTextField)).thenReturn(passwordField);

    when(passwordField.getText()).thenReturn(Editable.Factory.getInstance().newEditable("asd"));
    when(application.getPassword()).thenReturn("asd");

    sut = new ScreenLockPresenterImpl(application, rootView, screenLockListener,
            logoutButtonClickListener);
}


@Test
public void testOnClickWhenOk() {
    sut.onClick(null);

    verify(passwordField).getText();
    verify(screenLockListener).unLock();
}

我认为这就是您要寻找的内容: when(passwordField.getText()).thenReturn(Editable.Factory.getInstance().newEditable("asd"));

于 2017-07-19T21:40:38.360 回答