Unit testing for EditTextView. How should I do it?

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

}

The error I get is this:

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)

How can I check this? How in, how to set values ​​for EditText so that I can check it here?

The documentation is a bit scattered.

+3
source share
5 answers

It works just fine. Its just saying that your test case failed because the text in the EditText does not match the text being compared to. try it /..

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

How can I check this? Like in, how to set values ​​for EditText so that I can check it here?

This does not make much sense for the fake test EditText.setText (), however, if you insist, do something like this:

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

Hope this helps.

+6

, , .

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

+1

, 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);
                    }
                }
        );
    }
}

Then you can call it inside your 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());
}

Hope this helps

0
source

I know this is a very old post, how about this solution:

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

I think this is what you are looking for: when (passwordField.getText ()) thenReturn (Editable.Factory.getInstance () newEditable ("ASD").) ;.

0
source

All Articles