Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing for EditTextView. How do I do it?

Tags:

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

}

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 do I test it then? As in, how do I set the values for the EditText so that I can test it here?

The documentation is a bit scattered.

like image 226
Hick Avatar asked Feb 20 '23 18:02

Hick


1 Answers

How do I test it then? As in, how do I set the values for the EditText so that I can test it here?

It doesn't make much sense to dummy test EditText.setText() method, 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.

like image 122
yorkw Avatar answered Mar 08 '23 00:03

yorkw