EditTextView的单元测试。我该怎么做?

时间:2012-04-18 10:43:52

标签: android junit

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的值,以便我可以在这里测试它?

文档有点分散。

5 个答案:

答案 0 :(得分: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());
}

希望这有帮助。

答案 1 :(得分:1)

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

     @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());
                }

答案 2 :(得分:1)

这项技术有一个很好的好处,可以在测试运行时看到输入的测试字符串。

此方法将字符串写入editText元素,其中password是您的测试字符串。它应该添加到instrumentation测试类(扩展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

答案 3 :(得分: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());
}

希望这有帮助

答案 4 :(得分: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();
}

我认为这就是你要找的东西: 的当(passwordField.getText())thenReturn。(Editable.Factory.getInstance()newEditable(&#34; ASD&#34));